-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
73 lines (55 loc) · 1.74 KB
/
part2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from collections import Counter
CARDS = ['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
def get_type(cards: str) -> int:
cards = Counter(cards)
jokers = cards['J']
# Five of a kind
for card in CARDS:
if cards[card] <= 5 <= cards[card] + jokers:
return 6
# Four of a kind
for card in CARDS:
if cards[card] <= 4 <= cards[card] + jokers:
return 5
# Full house
for card1 in CARDS:
if cards[card1] <= 3 <= cards[card1] + jokers:
jokers_used = 3 - cards[card1]
for card2 in CARDS:
if card1 != card2 and cards[card2] <= 2 <= cards[card2] + max(0, jokers - jokers_used):
return 4
# Three of a kind
for card in CARDS:
if cards[card] <= 3 <= cards[card] + jokers:
return 3
# Two pairs
pairs = 0
jokers_used = 0
for card in CARDS:
if cards[card] <= 2 <= cards[card] + max(0, jokers - jokers_used):
pairs += 1
jokers_used += 2 - cards[card]
if pairs == 2:
return 2
# One pair
if pairs == 1:
return 1
# High card
return 0
def hand_value(hand: str) -> tuple[int]:
return tuple(CARDS.index(card) if card in CARDS else len(CARDS) for card in hand[0])
with open('input.txt') as file:
hands = [
[cards, int(bid)]
for cards, bid in map(str.split, filter(None, map(str.strip, file)))
]
hands_by_type = [[] for _ in range(7)]
for cards, bid in hands:
hands_by_type[get_type(cards)].append((cards, bid))
answer = 0
rank = 1
for cur_hands in hands_by_type:
for cards, bid in sorted(cur_hands, key=hand_value, reverse=True):
answer += bid * rank
rank += 1
print(answer)