From 99c2fa008698b247d8d7636f6e12b61f39ad7fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=ADle=20Ekaterin=20Liszka?= Date: Wed, 6 Dec 2023 23:52:50 -0800 Subject: [PATCH] AoC 2023 day 7 --- 7/a.py | 71 ++++++++++++++++++++++++++++++++++++++++++++ 7/b.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100755 7/a.py create mode 100755 7/b.py diff --git a/7/a.py b/7/a.py new file mode 100755 index 0000000..3a29408 --- /dev/null +++ b/7/a.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +from collections import Counter +from enum import IntEnum, auto + +order = '23456789TJQKA' + +class Hand(IntEnum): + HighCard = auto() + OnePair = auto() + TwoPair = auto() + ThreeKind = auto() + FullHouse = auto() + FourKind = auto() + FiveKind = auto() + +def rank_card(card): + return order.find(card) + +def evaluate(cards): + co = Counter(cards) + common = co.most_common() + ret = None + if common[0][1] == 5: + ret = Hand.FiveKind + elif common[0][1] == 4: + ret = Hand.FourKind + elif common[0][1] == 3 and common[1][1] == 2: + ret = Hand.FullHouse + elif common[0][1] == 3: + ret = Hand.ThreeKind + elif common[0][1] == 2 and common[1][1] == 2: + ret = Hand.TwoPair + elif common[0][1] == 2: + ret = Hand.OnePair + else: + ret = Hand.HighCard + + ret = [int(ret)] + ret.extend([rank_card(card) for card in cards]) + + return tuple(ret) + +def rank_hand(hand): + return evaluate(hand) + +hands = {} +bids = {} +value = {} + +with open('input.txt', mode='r') as f: + for line in f.readlines(): + line = line[:-1] + + hand, bid = line.split(' ', maxsplit=1) + bid = int(bid) + hands[hand] = hand + bids[hand] = bid + value[rank_hand(hand)] = hand + +ranks = list(value.keys()) +ranks.sort() + +ranking = {k: ranks.index(k) + 1 for k in value.keys()} +winnings = {k: bids[k] * ranking[v] for v, k in value.items()} + +for i in range(len(ranks)): + hand = value[ranks[i]] + print('hand', hand, 'is rank', i + 1, '=', ranks[i], f'bidding {bids[hand]}, and wins', winnings[hand]) + +print(sum(winnings.values())) diff --git a/7/b.py b/7/b.py new file mode 100755 index 0000000..35b003a --- /dev/null +++ b/7/b.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +from collections import Counter +from enum import IntEnum, auto + +order = 'J23456789TQKA' + +class Hand(IntEnum): + HighCard = auto() + OnePair = auto() + TwoPair = auto() + ThreeKind = auto() + FullHouse = auto() + FourKind = auto() + FiveKind = auto() + +def rank_card(card): + return order.find(card) + +def evaluate(cards): + co = Counter(cards) + common = co.most_common() + ret = None + + if len(common) == 5: + if 'J' not in cards: + ret = Hand.HighCard # A2345 + else: + ret = Hand.OnePair # JA234 + elif len(common) == 4: + if 'J' not in cards: + ret = Hand.OnePair # AA234 + else: + ret = Hand.ThreeKind # JAA23 or JJA23 + elif len(common) == 3: + if 'J' not in cards: + if common[0][1] == 3: + if common[1][1] == 2: + ret = Hand.FullHouse # AAA22 + else: + ret = Hand.ThreeKind # AAA23 + elif common[0][1] == common[1][1]: + ret = Hand.TwoPair # AA223 + else: + if common[0][1] == 3: # JJJ23 or 222J3 + ret = Hand.FourKind + elif common[1][1] == 2 and common[2][0] != 'J': + ret = Hand.FourKind # JJ223 or 22JJ3 + else: # 2233J + ret = Hand.FullHouse + elif len(common) == 2: + if 'J' not in cards: + if common[0][1] == 4: + ret = Hand.FourKind + else: + ret = Hand.FullHouse + else: # JJJ22 or JJJJ2 + ret = Hand.FiveKind + else: + ret = Hand.FiveKind + + ret = [int(ret)] + ret.extend([rank_card(card) for card in cards]) + + return tuple(ret) + +def rank_hand(hand): + return evaluate(hand) + +hands = {} +bids = {} +value = {} + +with open('input.txt', mode='r') as f: + for line in f.readlines(): + line = line[:-1] + + hand, bid = line.split(' ', maxsplit=1) + bid = int(bid) + hands[hand] = hand + bids[hand] = bid + value[rank_hand(hand)] = hand + +ranks = list(value.keys()) +ranks.sort() + +ranking = {k: ranks.index(k) + 1 for k in value.keys()} +winnings = {k: bids[k] * ranking[v] for v, k in value.items()} + +for i in range(len(ranks)): + hand = value[ranks[i]] + print('hand', hand, 'is rank', i + 1, '=', ranks[i], f'bidding {bids[hand]}, and wins', winnings[hand]) + +print(sum(winnings.values()))