Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
π Thread is locked until there's at least 100 2 star entries on the global leaderboard
π Thread has been unlocked after around 20 mins
I wrote some nice code for sorting poker hands, just defining the < and == operations for my CardSet and Hand types, and letting the standard library's sort function handle the rest.
It was quite frustrating to be told that my answer was wrong, though. I dumped the full sorted hand list and checked it manually to make sure everything was working properly, and it was. Wasted a few hours trying to figure out what was wrong. Ended up grabbing someone else's code and running it in order to compare the resulting hand list. Theirs was clearly ordered wrong, but somehow ended up with the correct answer?
Turns out that Camel Cards isn't Poker. -_-
Rather than rewrite my code entirely, I settled on some slightly ugly hacks to make it work for Camel Cards, and to handle the wildcards in part 2.
Hi there! Looks like you linked to a Lemmy community using a URL instead of its name, which doesn't work well for people on different instances. Try fixing it like this: !nim@programming.dev
I'm glad I took the time to read the directions very carefully before starting coding :-)
Top Tip: my ranking of hand types relies on the fact that if you count instances of each face and sort the resulting list from high to low, you get a list that when compared with lists from other hands gives an exact correspondence with the order of the hand types as defined, so no need for a bunch of if/thens, just
var type = Multiset.from(hand).counts.sorted(descending).join('');
Otherwise it should all be pretty self-explanatory apart from where I chose to map card rank to hex digits in order to facilitate sorting, so 'b' means 'J'!
int descending(T a, T b) => b.compareTo(a);
var cToH = " 23456789TJQKA"; // used to map card rank to hex for sorting.
handType(List hand, {wildcard = false}) {
var type = Multiset.from(hand).counts.sorted(descending).join('');
var i = hand.indexOf('b');
return (!wildcard || i == -1)
? type
: '23456789acde'
.split('')
.map((e) => handType(hand.toList()..[i] = e, wildcard: true))
.fold(type, (s, t) => s.compareTo(t) >= 0 ? s : t);
}
solve(List lines, {wildcard = false}) => lines
.map((e) {
var l = e.split(' ');
var hand =
l.first.split('').map((e) => cToH.indexOf(e).toRadixString(16));
var type = handType(hand.toList(), wildcard: wildcard);
if (wildcard) hand = hand.map((e) => e == 'b' ? '0' : e);
return (hand.join(), type, int.parse(l.last));
})
.sorted((a, b) {
var c = a.$2.compareTo(b.$2);
return (c == 0) ? a.$1.compareTo(b.$1) : c;
})
.indexed(offset: 1)
.map((e) => e.value.$3 * e.index)
.sum;
part1(List lines) => solve(lines);
part2(List lines) => solve(lines, wildcard: true);
It works, but even I can't understand this code any more as I'm well into my second beer, so don't put this into production, okay? (Run it here if you dare.)
This wasn't too bad. Had a worried moment when the part 2 solution took more than half a second. Maybe a better solution that brute forcing all the joker combinations, but it worked.
Python
import re
import argparse
import itertools
from enum import Enum
rule_jokers_enabled = False
class CardType(Enum):
HighCard = 1
OnePair = 2
TwoPair = 3
ThreeOfAKind = 4
FullHouse = 5
FourOfAKind = 6
FiveOfAKind = 7
class Hand:
def __init__(self,cards:str,bid:int) -> None:
self.cards = cards
self.bid = int(bid)
if rule_jokers_enabled:
self.type = self._find_type_joker(cards)
else:
self.type = self._find_type(cards)
def _find_type(self,cards:str) -> CardType:
# group cards based on card counts
card_list = [*cards]
card_list.sort()
grouping = itertools.groupby(card_list,lambda x:x)
lengths = [len(list(x[1])) for x in grouping]
if 5 in lengths:
return CardType.FiveOfAKind
if 4 in lengths:
return CardType.FourOfAKind
if 3 in lengths and 2 in lengths:
return CardType.FullHouse
if 3 in lengths:
return CardType.ThreeOfAKind
if len([x for x in lengths if x == 2]) == 2:
return CardType.TwoPair
if 2 in lengths:
return CardType.OnePair
return CardType.HighCard
def _find_type_joker(self,cards:str) -> CardType:
try:
joker_i = cards.index("J")
except ValueError:
return self._find_type(cards)
current_value = CardType.HighCard
for new_card in [*(valid_card_list())]:
if new_card == "J":
continue
test_cards = list(cards)
test_cards[joker_i] = new_card
new_value = self._find_type_joker("".join(test_cards))
if new_value.value > current_value.value:
current_value = new_value
return current_value
def sort_string(self):
v = str(self.type.value) + ":" + "".join(["abcdefghijklmnoZ"[card_value(x)] for x in [*self.cards]])
return v
def __repr__(self) -> str:
return f""
def valid_card_list() -> str:
if rule_jokers_enabled:
return "J23456789TQKA"
return "23456789TJQKA"
def card_value(char:chr):
return valid_card_list().index(char)
def main(line_list: list):
hand_list = list()
for l in line_list:
card,bid = re.split(' +',l)
hand = Hand(card,bid)
hand_list.append(hand)
#print(hand.sort_string())
hand_list.sort(key=lambda x: x.sort_string())
print(hand_list)
rank_total = 0
rank = 1
for single_hand in hand_list:
rank_total += rank * single_hand.bid
rank += 1
print(f"total {rank_total}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="day 1 solver")
parser.add_argument("-input",type=str)
parser.add_argument("-part",type=int)
args = parser.parse_args()
if args.part == 2:
rule_jokers_enabled = True
filename = args.input
if filename == None:
parser.print_help()
exit(1)
file = open(filename,'r')
main([line.rstrip('\n') for line in file.readlines()])
file.close()
I barely registered a difference between part 1 and part 2.
Part 1: 00:00:00.0018302
Part 2: 00:00:00.0073136
I suppose it took about 3.5 times as long, but I didn't notice :P
Edit: I realize that I made the implicit assumption in my solution that it doesn't make sense to have multiple jokers be interpreted as different values. i.e., The hand with the maximum value will have all Jokers interpreted as the same other card. I think that is true though. It worked out for me anyway.
Yea I was thinking there might be a simplification trick, but also figured "there can't be that many combinations right?" I suspect that was probably an intended optimisation.
I think one doesn't need to generate all combinations. All combinations using cards already present in the hand should be enough (since a joker can only increase the value of the hand by being grouped with existing cards (since in this game having four of a kind is always better than having any hand with a four of a kind/full house and having 3 is always better than any hand with pairs, and having a pair is better than any card without any cards of the same kind)). This massively decreases the amount of combinations needed to be generated per jokery hand.
Part 1 is just a sorting problem. Nim's standard library supports sorting with custom compare functions, so I only had to implement cmp() for my custom type and I was done in no time.
To get the star in Part 2 I was generating every possible combination of card hands with Jokers replaced by other cards. It was pretty fast, under a second. Didn't figure out the deterministic method by myself, but coded it after couple hints from Nim Discord people.
Didn't expect an easy challenge for today, but was pleasantly surprised. No weird edge cases, no hidden traps, puzzle text was easy to understand and input parsing is painless.
Total runtime: 1 ms
Puzzle rating: Almost Pefect 9/10
Code: day_07/solution.nim
Code (note: doesn't currently display correctly on Lemmy website)
use v6;
sub MAIN($input) {
my $file = open $input;
grammar CamelCards {
token TOP { +%"\n" "\n"*}
token row { " " }
token hand { \S+ }
token bid { \d+ }
}
my $camel-cards = CamelCards.parse($file.slurp);
my @rows = $camel-cards.map({ (..Str, ..Int) });
my @ranked-rows1 = @rows.sort({hand-strength($_[0], &hand-type-strength1, '23456789TJQKA'.comb)});
my $part-one-solution = (@ranked-rows1Β»[1] Z* 1..*).sum;
say "part 1: $part-one-solution";
my @ranked-rows2 = @rows.sort({hand-strength($_[0], &hand-type-strength2, 'J23456789TQKA'.comb)});
my $part-two-solution = (@ranked-rows2Β»[1] Z* 1..*).sum;
say "part 2: $part-two-solution";
}
sub hand-strength($hand, &hand-type-strength, @card-strengths) {
my $strength = &hand-type-strength($hand);
for $hand.comb -> $card {
$strength = $strength +< 8 + @card-strengths.first({ $_ eq $card }, :k);
}
return $strength;
}
sub hand-type-strength1($hand) {
my @sorted = $hand.comb.sort;
my @runs = [1];
my $card = @sorted[0];
for @sorted[1..*] -> $new-card {
if $new-card eq $card {
@runs.tail += 1;
} else {
@runs.push(1);
$card = $new-card;
}
}
return do given @runs.sort {
when .[0] == 5 { 6 } # Five of a kind
when .[1] == 4 { 5 } # Four of a kind
when .[1] == 3 { 4 } # Full House
when .[2] == 3 { 3 } # Three of a kind
when .[1] == 2 { 2 } # Two pair
when .[3] == 2 { 1 } # One pair
default { 0 } # High card
};
}
sub hand-type-strength2($hand) {
my @sorted = $hand.comb.grep(none /J/).sort;
if @sorted.elems == 0 {
return 6;
} else {
my @runs = [1];
my $card = @sorted[0];
for @sorted[1..*] -> $new-card {
if $new-card eq $card {
@runs.tail += 1;
} else {
@runs.push(1);
$card = $new-card;
}
}
@runs.=sort;
@runs.tail += 5 - @sorted.elems;
return do given @runs {
when .[0] == 5 { 6 } # Five of a kind
when .[1] == 4 { 5 } # Four of a kind
when .[1] == 3 { 4 } # Full House
when .[2] == 3 { 3 } # Three of a kind
when .[1] == 2 { 2 } # Two pair
when .[3] == 2 { 1 } # One pair
default { 0 } # High card
};
}
}
This was fun. More enjoyable than I initially thought (though I've done card sorting code before).
Part 1
This was pretty straightforward: create a histogram of the cards in each hand to determine their type, and if there is a tie-breaker, compare each card pairwise. I use the Counter class from collections to do the counting, and then had a dictionary/table to convert labels to numeric values for comparison. I used a very OOP approach and wrote a magic method for comparing hands and used that with Python's builtin sort. I even got to use Enum!
LABELS = {l: v for v, l in enumerate('23456789TJQKA', 2)}
class HandType(IntEnum):
FIVE_OF_A_KIND = 6
FOUR_OF_A_KIND = 5
FULL_HOUSE = 4
THREE_OF_A_KIND = 3
TWO_PAIR = 2
ONE_PAIR = 1
HIGH_CARD = 0
class Hand:
def __init__(self, cards=str, bid=str):
self.cards = cards
self.bid = int(bid)
counts = Counter(self.cards)
self.type = (
HandType.FIVE_OF_A_KIND if len(counts) == 1 else
HandType.FOUR_OF_A_KIND if len(counts) == 2 and any(l for l, count in counts.items() if count == 4) else
HandType.FULL_HOUSE if len(counts) == 2 and any(l for l, count in counts.items() if count == 3) else
HandType.THREE_OF_A_KIND if len(counts) == 3 and any(l for l, count in counts.items() if count == 3) else
HandType.TWO_PAIR if len(counts) == 3 and any(l for l, count in counts.items() if count == 2) else
HandType.ONE_PAIR if len(counts) == 4 and any(l for l, count in counts.items() if count == 2) else
HandType.HIGH_CARD
)
def __lt__(self, other):
if self.type == other.type:
for s_label, o_label in zip(self.cards, other.cards):
if LABELS[s_label] == LABELS[o_label]:
continue
return LABELS[s_label] < LABELS[o_label]
return False
return self.type < other.type
def __repr__(self):
return f'Hand(cards={self.cards},bid={self.bid},type={self.type})'
def read_hands(stream=sys.stdin) -> list[Hand]:
return [Hand(*line.split()) for line in stream]
def main(stream=sys.stdin) -> None:
hands = sorted(read_hands(stream))
winnings = sum(rank * hand.bid for rank, hand in enumerate(hands, 1))
print(winnings)
Part 2
For the second part, I just had to add some post-processing code to convert the jokers into actual cards. The key insight is to find the highest and most numerous non-Joker card and convert all the Jokers to that card label.
This had two edge cases that tripped me up:
'JJJJJ': There is no other non-Joker here, so I messed up and ranked this the lowest because I ended up removing all counts.
'JJJ12': This also messed me up b/c the Joker was the most numerous card, and I didn't handle that properly.
Once I fixed the post-processing code though, everything else remained the same. Below, I only show the parts that changed from Part A.
LABELS = {l: v for v, l in enumerate('J23456789TQKA', 1)}
...
class Hand:
def __init__(self, cards=str, bid=str):
self.cards = cards
self.bid = int(bid)
counts = Counter(self.cards)
if 'J' in counts and len(counts) > 1:
max_label = max(set(counts) - {'J'}, key=lambda l: (counts[l], LABELS[l]))
counts[max_label] += counts['J']
del counts['J']
self.type = (...)
As with the previous days: I'll only post the solution and parsing, not the dependencies I've put into separate files. For the full source code, please see github.
The key idea for part 2 was that
Spoiler
it doesn't make any sense to pick different cards for the jokers, and that it's always the highest score to assign all jokers to the most frequent card.
Solution
inductive Card
| two
| three
| four
| five
| six
| seven
| eight
| nine
| ten
| jack
| queen
| king
| ace
deriving Repr, Ord, BEq
inductive Hand
| mk : Card β Card β Card β Card β Card β Hand
deriving Repr, BEq
private inductive Score
| highCard
| onePair
| twoPair
| threeOfAKind
| fullHouse
| fourOfAKind
| fiveOfAKind
deriving Repr, Ord, BEq
-- we need countCards in part 2 again, but there it has different types
private class CardList (Ξ· : Type) (Ο : outParam Type) where
cardList : Ξ· β List Ο
-- similarly, we can implement Ord in terms of CardList and Score
private class Scorable (Ξ· : Type) where
score : Ξ· β Score
private instance : CardList Hand Card where
cardList := Ξ»
| .mk a b c d e => [a,b,c,d,e]
private def countCards {Ξ· Ο : Type} (input :Ξ·) [CardList Ξ· Ο] [Ord Ο] [BEq Ο] : List (Nat Γ Ο) :=
let ordered := (CardList.cardList input).quicksort
let helper := Ξ» (a : List (Nat Γ Ο)) (c : Ο) β¦ match a with
| [] => [(1, c)]
| a :: as =>
if a.snd == c then
(a.fst + 1, c) :: as
else
(1, c) :: a :: as
List.quicksortBy (Β·.fst > Β·.fst) $ ordered.foldl helper []
private def evaluateCountedCards : (l : List (Nat Γ Ξ±)) β Score
| [_] => Score.fiveOfAKind -- only one entry means all cards are equal
| (4,_) :: _ => Score.fourOfAKind
| [(3,_), (2,_)] => Score.fullHouse
| (3,_) :: _ => Score.threeOfAKind
| [(2,_), (2,_), _] => Score.twoPair
| (2,_) :: _ => Score.onePair
| _ => Score.highCard
private def Hand.score (hand : Hand) : Score :=
evaluateCountedCards $ countCards hand
private instance : Scorable Hand where
score := Hand.score
instance {Ο Ο : Type} [Scorable Ο] [CardList Ο Ο] [Ord Ο] : Ord Ο where
compare (a b : Ο) :=
let comparedScores := Ord.compare (Scorable.score a) (Scorable.score b)
if comparedScores != Ordering.eq then
comparedScores
else
Ord.compare (CardList.cardList a) (CardList.cardList b)
private def Card.fromChar? : Char β Option Card
| '2' => some Card.two
| '3' => some Card.three
| '4' => some Card.four
| '5' => some Card.five
| '6' => some Card.six
| '7' => some Card.seven
| '8' => some Card.eight
| '9' => some Card.nine
| 'T' => some Card.ten
| 'J' => some Card.jack
| 'Q' => some Card.queen
| 'K' => some Card.king
| 'A' => some Card.ace
| _ => none
private def Hand.fromString? (input : String) : Option Hand :=
match input.toList.mapM Card.fromChar? with
| some [a, b, c, d, e] => Hand.mk a b c d e
| _ => none
abbrev Bet := Nat
structure Player where
hand : Hand
bet : Bet
deriving Repr
def parse (input : String) : Except String (List Player) := do
let lines := input.splitOn "\n" |> List.map String.trim |> List.filter String.notEmpty
let parseLine := Ξ» (line : String) β¦
if let [hand, bid] := line.split Char.isWhitespace |> List.map String.trim |> List.filter String.notEmpty then
Option.zip (Hand.fromString? hand) (String.toNat? bid)
|> Option.map (uncurry Player.mk)
|> Option.toExcept s!"Line could not be parsed: {line}"
else
throw s!"Failed to parse. Line did not separate into hand and bid properly: {line}"
lines.mapM parseLine
def part1 (players : List Player) : Nat :=
players.quicksortBy (Ξ» p q β¦ p.hand < q.hand)
|> List.enumFrom 1
|> List.foldl (Ξ» r p β¦ p.fst * p.snd.bet + r) 0
------------------------------------------------------------------------------------------------------
-- Again a riddle where part 2 needs different data representation, why are you doing this to me? Why?
-- (Though, strictly speaking, I could just add "joker" to the list of cards in part 1 and treat it special)
private inductive Card2
| joker
| two
| three
| four
| five
| six
| seven
| eight
| nine
| ten
| queen
| king
| ace
deriving Repr, Ord, BEq
private def Card.toCard2 : Card β Card2
| .two => Card2.two
| .three => Card2.three
| .four => Card2.four
| .five => Card2.five
| .six => Card2.six
| .seven => Card2.seven
| .eight => Card2.eight
| .nine => Card2.nine
| .ten => Card2.ten
| .jack => Card2.joker
| .queen => Card2.queen
| .king => Card2.king
| .ace => Card2.ace
private inductive Hand2
| mk : Card2 β Card2 β Card2 β Card2 β Card2 β Hand2
deriving Repr
private def Hand.toHand2 : Hand β Hand2
| Hand.mk a b c d e => Hand2.mk a.toCard2 b.toCard2 c.toCard2 d.toCard2 e.toCard2
instance : CardList Hand2 Card2 where
cardList := Ξ»
| .mk a b c d e => [a,b,c,d,e]
private def Hand2.score (hand : Hand2) : Score :=
-- I could be dumb here and just let jokers be any other card, but that would be really wasteful
-- Also, I'm pretty sure there is no combination that would benefit from jokers being mapped to
-- different cards.
-- and, even more important, I think we can always map jokers to the most frequent card and are
-- still correct.
let counted := countCards hand
let (jokers, others) := counted.partition Ξ» e β¦ e.snd == Card2.joker
let jokersReplaced := match jokers, others with
| (jokers, _) :: _ , (a, ac) :: as => (a+jokers, ac) :: as
| _ :: _, [] => jokers
| [], others => others
evaluateCountedCards jokersReplaced
private instance : Scorable Hand2 where
score := Hand2.score
private structure Player2 where
bet : Bet
hand2 : Hand2
def part2 (players : List Player) : Nat :=
let players := players.map Ξ» p β¦
{bet := p.bet, hand2 := p.hand.toHand2 : Player2}
players.quicksortBy (Ξ» p q β¦ p.hand2 < q.hand2)
|> List.enumFrom 1
|> List.foldl (Ξ» r p β¦ p.fst * p.snd.bet + r) 0
Getting the count of each card, the two highest counts easily show what type of hand we have. For part 2 I just added the number of jokers to the highest count.
I spent some time messing around with generics to minimize code duplication between the solutions to both parts. I could have absolutely just copied everything and made small changes, but now my solution is generic over puzzle parts.
I tried to do this one as quickly as possible, so the code is more messy than I would prefer, but it works, and I don't think the solution is too bad overall.
Edit: I went back and changed it to be a bit better. Here are my new solutions:
Two days, a few failed solutions, some misread instructions, and a lot of
manually parsing output data and debugging silly tiny mistakes... but it's
finally done. I don't really wanna talk about it.