From 1becb4b4bf115de498d2cfe90d85c63a964bd063 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Fri, 8 Dec 2023 10:21:36 +0100 Subject: [PATCH] day 7 part 1 --- day7/day7.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/day7/day7.py b/day7/day7.py index 887d8ed..637f32c 100644 --- a/day7/day7.py +++ b/day7/day7.py @@ -3,27 +3,24 @@ from collections import Counter def calculate_rank(hand): card_ranks = {'2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12, 'A': 13} # substitute cards with their ranks - hand = sorted(card_ranks.get(c, None) or c for c in hand)[::-1] + hand = [card_ranks[c] for c in hand] cnt = Counter(hand) - cnt_values = sorted(cnt.values()) - rank = { - "5": 10, - "14": 9, - "23": 8, - "113": 7, - "122": 6, - "1112": 5, - "11111": 4, - }["".join(map(str, cnt_values))] - # return rank and sorted hand as a tiebreaker + rank = 0 + match sorted(cnt.values()): + case [5]: rank = 7 + case [1, 4]: rank = 6 + case [2, 3]: rank = 5 + case [1, 1, 3]: rank = 4 + case [1, 2, 2]: rank = 3 + case [1, 1, 1, 2]: rank = 2 + case [1, 1, 1, 1, 1]: rank = 1 + return (rank, hand) def part1(inp): total = 0 hands = [l.strip().split() for l in inp.strip().split("\n")] - print(hands[-10: -1]) - breakpoint() hands = sorted(hands, key=lambda hb: calculate_rank(hb[0])) for rank, hand in enumerate(hands): hand, bid = hand @@ -39,11 +36,12 @@ if __name__ == "__main__": KTJJT 220 QQQJA 483 """ - assert part1(sample_input) == 6440 + res = part1(sample_input) + print(f"part 1 example: {res}") + assert res == 6440 import sys if len(sys.argv) == 2: with open(sys.argv[1]) as f: res = part1(f.read()) print(f"Part 1, res={res}") -