Day 7: Camel Cards
Megathread guidelines
- 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)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- 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
Nim
I wrote some nice code for sorting poker hands, just defining the
<
and==
operations for myCardSet
andHand
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: [email protected]
Dart
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’s Uiua time!
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.)
{"32T3K 765" "T55J5 684" "KK677 28" "KTJJT 220" "QQQJA 483"} StoInt ← /(+×10)▽×⊃(≥0)(≤9).-@0 ToHex ← ⊏:" 23456789abcde"⊗:" 23456789TJQKA" ToHexJ ← ⊏:" 23456789a0cde"⊗:" 23456789TJQKA" # A hand of "311" with one J will have same rank as "41" # Dots indicate impossible hands. Rankings ← { {"11111" "2111" "221" "311" "32" "41" "5"} # 0 {"..." "11111" ".." "2111" "221" "311" "41"} # 1 {"..." "....." ".." "2111" "..." "221" "32"} # 2 {"..." "....." ".." "...." "..." "311" "32"} # 3 {"..." "....." ".." "...." "..." "..." "41"} # 4 {"..." "....." ".." "...." "..." "..." "5"} # 5 } RankHand ← ( +@0⊏⍖.⊕⧻⊛⊢⍉⇌⊕∘⍖... # Count instances, sort desc, to string ⊗⊃⊢(⊔⊡:Rankings/+=@0⊢↘1)⊟∩□ # Use table to get ranking ) ScoreHands! ← ( ≡(⊐⊟⊓(⊐⊟RankHand.^1⊔)∘⍘⊟) # Rank every hand /+/×⊟+1⇡⧻.∵⊔≡(⊢↘1)⊏⍏≡⊢. # Sort based on rankings ) ⍉⊟⊓∘(∵StoInt)⍘⊟⍉≡(⊐⊜∘≠@\s.) # Parse input ⊃(ScoreHands!ToHex)(ScoreHands!ToHexJ)
How do you debug this language??
Lots and lots of print statements :-)
Lord have mercy upon our souls
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.
Scala3
val tiers = List(List(1, 1, 1, 1, 1), List(1, 1, 1, 2), List(1, 2, 2), List(1, 1, 3), List(2, 3), List(1, 4), List(5)) val cards = List('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A') def cardValue(base: Long, a: List[Char], cards: List[Char]): Long = a.foldLeft(base)(cards.size * _ + cards.indexOf(_)) def hand(a: List[Char]): List[Int] = a.groupMapReduce(s => s)(_ => 1)(_ + _).values.toList.sorted def power(a: List[Char]): Long = cardValue(tiers.indexOf(hand(a)), a, cards) def power3(a: List[Char]): Long = val x = hand(a.filter(_ != 'J')) val t = tiers.lastIndexWhere(x.zipAll(_, 0, 0).forall(_ <= _)) cardValue(t, a, 'J'::cards) def win(a: List[String], pow: List[Char] => Long) = a.flatMap{case s"$hand $bid" => Some((pow(hand.toList), bid.toLong)); case _ => None} .sorted.map(_._2).zipWithIndex.map(_ * _.+(1)).sum def task1(a: List[String]): Long = win(a, power) def task2(a: List[String]): Long = win(a, power3)
Nim
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.nimRaku
My hand-type strength calculations could probably be trimmed down a bit. I didn’t hit any big issues today.
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 }; } }
Python
Part 1: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day 7/part1.py
Code
import os filePath = os.path.dirname(os.path.realpath(__file__)) inputFilePath = filePath + "\\adventofcode.com_2023_day_7_input.txt" # inputFilePath = filePath + "\\part1.txt" def typeSort(hand): cardCount = { "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "T": 0, "J": 0, "Q": 0, "K": 0, "A": 0 } for card in hand: cardCount[card] += 1 cardTotals = list(cardCount.values()) cardTotals.sort(reverse=True) if(cardTotals[0] == 5): return 6 elif(cardTotals[0] == 4): return 5 elif(cardTotals[0] == 3 and cardTotals[1] == 2): return 4 elif(cardTotals[0] == 3): return 3 elif(cardTotals[0] == 2 and cardTotals[1] == 2): return 2 elif(cardTotals[0] == 2): return 1 else: return 0 def bucketSort(camelCard): totalScore = 0 cardOrder = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"] hand = camelCard[0] totalScore += cardOrder.index(hand[4]) * 15 ** 1 totalScore += cardOrder.index(hand[3]) * 15 ** 2 totalScore += cardOrder.index(hand[2]) * 15 ** 3 totalScore += cardOrder.index(hand[1]) * 15 ** 4 totalScore += cardOrder.index(hand[0]) * 15 ** 5 return totalScore hands = [] bids = [] with open(inputFilePath) as inputFile: for line in inputFile: lineSplit = line.split() hand = lineSplit[0] bid = lineSplit[1] hands.append(hand) bids.append(bid) bids = [int(bid) for bid in bids] camelCards = list(zip(hands,bids)) typeBuckets = [[],[],[],[],[],[],[]] for camelCard in camelCards: hand = camelCard[0] typeScore = typeSort(hand) typeBuckets[typeScore].append(camelCard) finalCardSort = [] for bucket in typeBuckets: if(len(bucket) > 1): bucket.sort(key=bucketSort) for camelCard in bucket: finalCardSort.append(camelCard) camelScores = [] for camelIndex in range(len(finalCardSort)): scoreMultiplier = camelIndex + 1 camelCard = finalCardSort[camelIndex] camelScores.append(camelCard[1] * scoreMultiplier) print(sum(camelScores))
Part 2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day 7/part2.py
Code
import os filePath = os.path.dirname(os.path.realpath(__file__)) inputFilePath = filePath + "\\adventofcode.com_2023_day_7_input.txt" # inputFilePath = filePath + "\\part1.txt" def typeSort(hand): cardCount = { "J": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "T": 0, "Q": 0, "K": 0, "A": 0 } for card in hand: cardCount[card] += 1 jokerCount = cardCount["J"] cardCount["J"] = 0 cardTotals = list(cardCount.values()) cardTotals.sort(reverse=True) if(cardTotals[0] + jokerCount == 5): return 6 elif(cardTotals[0] + jokerCount == 4): return 5 elif( cardTotals[0] + jokerCount == 3 and cardTotals[1] == 2 or cardTotals[0] == 3 and cardTotals[1] + jokerCount == 2 ): return 4 elif(cardTotals[0] + jokerCount == 3): return 3 elif( cardTotals[0] + jokerCount == 2 and cardTotals[1] == 2 or cardTotals[0] == 2 and cardTotals[1] + jokerCount == 2 ): return 2 elif(cardTotals[0] + jokerCount == 2): return 1 else: return 0 def bucketSort(camelCard): totalScore = 0 cardOrder = ["J","2","3","4","5","6","7","8","9","T","Q","K","A"] hand = camelCard[0] totalScore += cardOrder.index(hand[4]) * 15 ** 1 totalScore += cardOrder.index(hand[3]) * 15 ** 2 totalScore += cardOrder.index(hand[2]) * 15 ** 3 totalScore += cardOrder.index(hand[1]) * 15 ** 4 totalScore += cardOrder.index(hand[0]) * 15 ** 5 return totalScore hands = [] bids = [] with open(inputFilePath) as inputFile: for line in inputFile: lineSplit = line.split() hand = lineSplit[0] bid = lineSplit[1] hands.append(hand) bids.append(bid) bids = [int(bid) for bid in bids] camelCards = list(zip(hands,bids)) typeBuckets = [[],[],[],[],[],[],[]] for camelCard in camelCards: hand = camelCard[0] typeScore = typeSort(hand) typeBuckets[typeScore].append(camelCard) finalCardSort = [] for bucket in typeBuckets: if(len(bucket) > 1): bucket.sort(key=bucketSort) for camelCard in bucket: finalCardSort.append(camelCard) camelScores = [] for camelIndex in range(len(finalCardSort)): scoreMultiplier = camelIndex + 1 camelCard = finalCardSort[camelIndex] camelScores.append(camelCard[1] * scoreMultiplier) print(sum(camelScores))
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:
Part 1 v2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day 7/part1v2.py
Code
import os filePath = os.path.dirname(os.path.realpath(__file__)) inputFilePath = filePath + "\\adventofcode.com_2023_day_7_input.txt" # inputFilePath = filePath + "\\part1.txt" CARD_ORDER = "23456789TJQKA" def typeSort(camelCard): cardCount = {} for card in CARD_ORDER: cardCount[card] = 0 hand = camelCard[0] for card in hand: cardCount[card] += 1 cardTotals = list(cardCount.values()) cardTotals.sort(reverse=True) if(cardTotals[0] == 5): return 6 elif(cardTotals[0] == 4): return 5 elif(cardTotals[0] == 3 and cardTotals[1] == 2): return 4 elif(cardTotals[0] == 3): return 3 elif(cardTotals[0] == 2 and cardTotals[1] == 2): return 2 elif(cardTotals[0] == 2): return 1 else: return 0 def handSort(camelCard): totalScore = 0 hand = camelCard[0] totalScore += CARD_ORDER.index(hand[4]) * 15 ** 1 totalScore += CARD_ORDER.index(hand[3]) * 15 ** 2 totalScore += CARD_ORDER.index(hand[2]) * 15 ** 3 totalScore += CARD_ORDER.index(hand[1]) * 15 ** 4 totalScore += CARD_ORDER.index(hand[0]) * 15 ** 5 return totalScore hands = [] bids = [] with open(inputFilePath) as inputFile: for line in inputFile: lineSplit = line.split() hand = lineSplit[0] bid = lineSplit[1] hands.append(hand) bids.append(int(bid)) camelCards = list(zip(hands,bids)) camelCards = sorted(camelCards, key=lambda x: (typeSort(x), handSort(x))) camelScores = [] for camelIndex in range(len(camelCards)): scoreMultiplier = camelIndex + 1 camelCard = camelCards[camelIndex] camelScores.append(camelCard[1] * scoreMultiplier) print(sum(camelScores))
Part 2 v2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day 7/part2v2.py
Code
import os filePath = os.path.dirname(os.path.realpath(__file__)) inputFilePath = filePath + "\\adventofcode.com_2023_day_7_input.txt" # inputFilePath = filePath + "\\part1.txt" CARD_ORDER = "J23456789TQKA" def typeSort(camelCard): cardCount = {} for card in CARD_ORDER: cardCount[card] = 0 hand = camelCard[0] for card in hand: cardCount[card] += 1 jokerCount = cardCount["J"] cardCount["J"] = 0 cardTotals = list(cardCount.values()) cardTotals.sort(reverse=True) if(cardTotals[0] + jokerCount == 5): return 6 elif(cardTotals[0] + jokerCount == 4): return 5 elif( cardTotals[0] + jokerCount == 3 and cardTotals[1] == 2 or cardTotals[0] == 3 and cardTotals[1] + jokerCount == 2 ): return 4 elif(cardTotals[0] + jokerCount == 3): return 3 elif( cardTotals[0] + jokerCount == 2 and cardTotals[1] == 2 or cardTotals[0] == 2 and cardTotals[1] + jokerCount == 2 ): return 2 elif(cardTotals[0] + jokerCount == 2): return 1 else: return 0 def handSort(camelCard): totalScore = 0 hand = camelCard[0] totalScore += CARD_ORDER.index(hand[4]) * 15 ** 1 totalScore += CARD_ORDER.index(hand[3]) * 15 ** 2 totalScore += CARD_ORDER.index(hand[2]) * 15 ** 3 totalScore += CARD_ORDER.index(hand[1]) * 15 ** 4 totalScore += CARD_ORDER.index(hand[0]) * 15 ** 5 return totalScore hands = [] bids = [] with open(inputFilePath) as inputFile: for line in inputFile: lineSplit = line.split() hand = lineSplit[0] bid = lineSplit[1] hands.append(hand) bids.append(int(bid)) camelCards = list(zip(hands,bids)) camelCards = sorted(camelCards, key=lambda x: (typeSort(x), handSort(x))) camelScores = [] for camelIndex in range(len(camelCards)): scoreMultiplier = camelIndex + 1 camelCard = camelCards[camelIndex] camelScores.append(camelCard[1] * scoreMultiplier) print(sum(camelScores))
Rust
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.
Language: Python
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 = (...)
-
C#
Not too bad - I just scored every hand for the first part so I could easily sort it.
For the second part I just brute forced the replacements for the hand type matchinge (first digit of score)
Task1
public class Day7Task1:IRunnable {
public static Dictionary CardValues = new Dictionary() { { '2', "01" }, { '3', "02" }, { '4', "03" }, { '5', "04" }, { '6', "05" }, { '7', "06" }, { '8', "07" }, { '9', "08" }, { 'T', "09" }, { 'J', "10" }, { 'Q', "11" }, { 'K', "12" }, { 'A', "13" } }; public void Run() { //var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt"); var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt"); var hands = inputLines.Select(line => { var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); return new Hand(split[0], split[1] ); }).ToList(); var sortedHands = hands.OrderBy(hand => hand.Score).ToList(); long resultValue = 0; for (int i = 1; i < hands.Count()+1; i++) { resultValue += i * sortedHands[i-1].Bid; } Console.WriteLine("Result:" + resultValue); } public class Hand { public Hand(string cards, string bid) { Cards = cards; Bid = int.Parse(bid); Score = GenerateScore(); } public string Cards { get; set; } public int Bid { get; set; } public long Score { get; } private long GenerateScore() { var resultString = new StringBuilder(); var cardGroups = Cards.GroupBy(c => c).ToList(); var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList(); if (cardGroups.Count() == 1) { resultString.Append("7"); } else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1)) { resultString.Append("6"); } else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2)) { resultString.Append("5"); } else if(cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3)) { resultString.Append("4"); } else if(cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1) { resultString.Append("3"); } else if(cardGroups.Count() == 4 ) { resultString.Append("2"); } else { resultString.Append("1"); } foreach (var card in Cards) { resultString.Append(Day7Task1.CardValues[card]); } Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString); return long.Parse(resultString.ToString()); } } }
Task2
public class Day7Task2:IRunnable { public static Dictionary CardValues = new Dictionary() { { '2', "01" }, { '3', "02" }, { '4', "03" }, { '5', "04" }, { '6', "05" }, { '7', "06" }, { '8', "07" }, { '9', "08" }, { 'T', "09" }, { 'J', "00" }, { 'Q', "11" }, { 'K', "12" }, { 'A', "13" } }; public void Run() { //var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt"); var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt"); var hands = inputLines.Select(line => { var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); return new Hand(split[0], split[1] ); }).ToList(); var sortedHands = hands.OrderBy(hand => hand.Score).ToList(); long resultValue = 0; for (int i = 1; i < hands.Count()+1; i++) { resultValue += i * sortedHands[i-1].Bid; } Console.WriteLine("Result:" + resultValue); } public class Hand { public Hand(string cards, string bid) { Cards = cards; Bid = int.Parse(bid); Score = GenerateScore(); } public string Cards { get; set; } public int Bid { get; set; } public long Score { get; } private long GenerateScore() { var generateFirstDigit = new Func(cards => { var cardGroups = cards.GroupBy(c => c).ToList(); var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList(); if (cardGroups.Count() == 1) { return 7; } else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1)) { return 6; } else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2)) { return 5; } else if (cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3)) { return 4; } else if (cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1) { return 3; } else if (cardGroups.Count() == 4) { return 2; } else { return 1; } }); var resultString = new StringBuilder(); var maxFistDigit = Day7Task2.CardValues.Keys.Select(card => generateFirstDigit(Cards.Replace('J', card))).Max(); resultString.Append(maxFistDigit); foreach (var card in Cards) { resultString.Append(Day7Task2.CardValues[card]); } Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString); return long.Parse(resultString.ToString()); } } }
Crystal
got stuck on both parts due to silly mistakes.
On the other hand I’m no longer behind!code
input = File.read("input.txt").lines rank = { 'J' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'T' => 10, # 'J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14 } hand = input.map do |line| split = line.split weights = split[0].chars.map {|c| rank[c]} {weights, split[1].to_i} end hand.sort! do |a, b| a_rank = get_rank(a[0], true) b_rank = get_rank(b[0], true) # puts "#{a}-#{a_rank} #{b}-#{b_rank}" next 1 if a_rank > b_rank next -1 if b_rank > a_rank val = 0 5.times do |i| val = 1 if a[0][i] > b[0][i] val = -1 if b[0][i] > a[0][i] break unless val == 0 end val end sum = 0 hand.each_with_index do |card, i| sum += card[1]*(i+1) end puts sum def get_rank(card : Array(Int32), joker = false ) : Float64 | Int32 aa = card.uniq if joker card = aa.map { |c| combo = card.map {|a| a == 1 ? c : a } {combo, get_rank(combo)} }.max_by {|a| a[1]}[0] aa = card.uniq end rank = 6 - aa.size case rank when 3 return 3.5 if card.count(aa[0]) == 3 return 3 if card.count(aa[0]) == 2 return 3 if card.count(aa[1]) == 2 return 3.5 when 4 return 4 if card.count(aa[0]) == 3 || card.count(aa[0]) == 2 return 4.5 else return rank end end
Factor on github (with comments and imports):
! hand: "A23A4" ! card: 'Q' ! hand-bid: { "A23A4" 220 } : card-key ( ch -- n ) "23456789TJQKA" index ; : five-kind? ( hand -- ? ) cardinality 1 = ; : four-kind? ( hand -- ? ) sorted-histogram last last 4 = ; : full-house? ( hand -- ? ) sorted-histogram { [ last last 3 = ] [ length 2 = ] } && ; : three-kind? ( hand -- ? ) sorted-histogram { [ last last 3 = ] [ length 3 = ] } && ; : two-pair? ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 3 = ] } && ; : one-pair? ( hand -- ? ) sorted-histogram { [ last last 2 = ] [ length 4 = ] } && ; : high-card? ( hand -- ? ) cardinality 5 = ; : type-key ( hand -- n ) [ 0 ] dip { [ high-card? ] [ one-pair? ] [ two-pair? ] [ three-kind? ] [ full-house? ] [ four-kind? ] [ five-kind? ] } [ dup empty? ] [ unclip pick swap call( h -- ? ) [ drop f ] [ [ 1 + ] 2dip ] if ] until 2drop ; :: (hand-compare) ( hand1 hand2 type-key-quot card-key-quot -- <=> ) hand1 hand2 type-key-quot compare dup +eq+ = [ drop hand1 hand2 [ card-key-quot compare ] { } 2map-as { +eq+ } without ?first dup [ drop +eq+ ] unless ] when ; inline : hand-compare ( hand1 hand2 -- <=> ) [ type-key ] [ card-key ] (hand-compare) ; : input>hand-bids ( -- hand-bids ) "vocab:aoc-2023/day07/input.txt" utf8 file-lines [ " " split1 string>number 2array ] map ; : solve ( hand-compare-quot -- ) '[ [ first ] bi@ @ ] input>hand-bids swap sort-with [ 1 + swap last * ] map-index sum . ; inline : part1 ( -- ) [ hand-compare ] solve ; : card-key-wilds ( ch -- n ) "J23456789TQKA" index ; : type-key-wilds ( hand -- n ) [ type-key ] [ "J" within length ] bi 2array { { { 0 1 } [ 1 ] } { { 1 1 } [ 3 ] } { { 1 2 } [ 3 ] } { { 2 1 } [ 4 ] } { { 2 2 } [ 5 ] } { { 3 1 } [ 5 ] } { { 3 3 } [ 5 ] } { { 4 2 } [ 6 ] } { { 4 3 } [ 6 ] } { { 5 1 } [ 6 ] } { { 5 4 } [ 6 ] } [ first ] } case ; : hand-compare-wilds ( hand1 hand2 -- <=> ) [ type-key-wilds ] [ card-key-wilds ] (hand-compare) ; : part2 ( -- ) [ hand-compare-wilds ] solve ;
Ruby
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day07/day07.rb
Gonna clean it up now, but pretty simple at the end of it all. Helps that ruby has several methods to make this dead simple, like
tally
,any?
,all?
, andzip
Cleaned up solution:
def get_score(tally) vals = tally.values map = { ->(x) { x.any?(5) } => 7, ->(x) { x.any?(4) } => 6, ->(x) { x.any?(3) && x.any?(2) } => 5, ->(x) { x.any?(3) && tally.all? { |_, v| v != 2 } } => 4, ->(x) { x.count(2) == 2 } => 3, ->(x) { x.one?(2) && tally.all? { |_, v| v <= 2 } } => 2, ->(x) { x.all?(1) } => 1, } map.find { |lambda, _| lambda.call(vals) }[1] end def get_ranking(lines, score_map, scores) lines.zip(scores).to_h.sort do |a, b| a_line, a_score = a b_line, b_score = b if a_score == b_score a_hand, _ = a_line.split b_hand, _ = b_line.split diff = a_hand.chars.zip(b_hand.chars).drop_while { |a, b| a == b }[0] card_1 = score_map.index(diff[0]) card_2 = score_map.index(diff[1]) card_1 <=> card_2 else a_score <=> b_score end end end def calculate_total_winnings(ranking) max_rank = ranking.size (1..max_rank).sum(0) do |rank| line = ranking[rank - 1] _, bid = line[0].split bid.to_i * rank end end score_map_p1 = %w[. . 2 3 4 5 6 7 8 9 T J Q K A] score_map_p2 = %w[. . J 2 3 4 5 6 7 8 9 T Q K A] execute(1) do |lines| scores = lines.map do |line| hand, _ = line.split tally = hand.split('').tally get_score tally end ranking = get_ranking(lines, score_map_p1, scores) calculate_total_winnings ranking end execute(2) do |lines| scores = lines.map do |line| hand, _ = line.split hand_split = hand.split('') tally = hand_split.tally if hand_split.any? { |c| c == 'J' } highest_non_j = tally.reject { |k, v| k == 'J' }.max_by { |k, v| v } if highest_non_j.nil? tally = { 'A': 5 } else tally[highest_non_j[0]] += tally['J'] end tally.delete('J') end get_score tally end ranking = get_ranking(lines, score_map_p2, scores) calculate_total_winnings(ranking) end
[language: Lean4]
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