2023 - Day 7 part 1
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
kleph 2023-12-08 20:10:35 +01:00
parent ed9e80e7c9
commit 020e322e3b
3 changed files with 1075 additions and 0 deletions

70
2023/7/7.py Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
# 2023 - Advent Of Code 7
# file = 'input_example.txt'
file = 'input.txt'
class Hand:
def __init__(self, cards, bet):
self.cards = cards
self.bet = bet
self.dict_cards = {}
def __repr__(self):
return repr(f'cards: {self.cards}')
def sort_cards(self):
self.dict_cards = sorted(self.dict_cards.items(), key=lambda x: x[1], reverse=True)
cards_order = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
hands = [Hand(line.split()[0], line.split()[1]) for line in input_lines]
print(hands)
for h in hands:
hand_dict = {k: 0 for k in cards_order}
for card in h.cards:
hand_dict[card] += 1
h.dict_cards = {k: v for k, v in hand_dict.items() if v != 0}
for h in hands:
h.sort_cards()
hands_5 = [h for h in hands if h.dict_cards[0][1] == 5]
# for h in hands_5:
# print(h)
sorted_5 = sorted(hands_5, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_4 = [h for h in hands if h.dict_cards[0][1] == 4]
sorted_4 = sorted(hands_4, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_full = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] == 2]
sorted_full = sorted(hands_full, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_3 = [h for h in hands if h.dict_cards[0][1] == 3 and h.dict_cards[1][1] != 2]
sorted_3 = sorted(hands_3, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_twopairs = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] == 2]
sorted_twopairs = sorted(hands_twopairs, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_onepair = [h for h in hands if h.dict_cards[0][1] == 2 and h.dict_cards[1][1] != 2]
sorted_onepair = sorted(hands_onepair, key=lambda x: [cards_order.index(c) for c in x.cards])
hands_high = [h for h in hands if h.dict_cards[0][1] == 1]
sorted_high = sorted(hands_high, key=lambda x: [cards_order.index(c) for c in x.cards])
sorted_hands = sorted_high + sorted_onepair + sorted_twopairs + sorted_3 + sorted_full + sorted_4 + sorted_5
acc = 0
for rank, h in enumerate(sorted_hands):
# print(f'{rank+1} {h.bet}')
print((rank + 1) * int(h.bet))
acc += (rank+1) * int(h.bet)
print(acc)

1000
2023/7/input.txt Normal file

File diff suppressed because it is too large Load diff

5
2023/7/input_example.txt Normal file
View file

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483