diff --git a/2023/python/day07.py b/2023/python/day07.py index a772582..45b9ae2 100644 --- a/2023/python/day07.py +++ b/2023/python/day07.py @@ -3,8 +3,12 @@ import shared from dataclasses import dataclass from collections import defaultdict from pprint import pprint +from typing import List + +#STRENGTHS = list(reversed("A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", "))) +STRENGTHS = "A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", ") +STRENGTHS_TO_WEIGHT = {k:idx for idx, k in enumerate(STRENGTHS)} -STRENGTHS = list(reversed("A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", "))) STRENGTH_HANDS = [ "Five of a Kind", "Four of a Kind", @@ -48,6 +52,11 @@ class Hand: cards = [c for c in self.cards] return determine_hand(self.cards) + def sort_cards(self): + return [STRENGTHS_TO_WEIGHT[x] for x in self.cards] + + def __lt__(self, other): + return self.sort_cards() > other.sort_cards() # @shared.profile @@ -57,9 +66,15 @@ def part1(rows): cards, bid = row.split() hand = Hand(cards=cards, bid=int(bid)) hands[hand.kind()].append(hand) - pprint(hands) - - + hands[hand.kind()] = list(sorted(hands[hand.kind()])) + in_order = [] + for kind in reversed(STRENGTH_HANDS): + in_order.extend(hands[kind]) + total = 0 + for rank, card in enumerate(in_order): + total += (rank+1)*card.bid + + print(total) # @shared.profile @@ -73,7 +88,6 @@ def main(): part1(rows) print("🕒", elapsed()) - rows = [row for row in shared.load_rows(1,True)] with shared.elapsed_timer() as elapsed: part2(rows) print("🕒", elapsed())