-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_camel_cards.py
67 lines (51 loc) · 1.76 KB
/
07_camel_cards.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
import functools
def read_data(filename):
with open(filename, mode='r') as file:
return [(line.split()[0], int(line.split()[1])) for line in file]
def process_hand(received_hand: str):
processed_hand = {}
for card in received_hand:
if card not in processed_hand:
processed_hand[card] = 1
else:
processed_hand[card] += 1
return dict(sorted(processed_hand.items(), key=lambda x: x[1], reverse=True))
def calculate_hand_value(processed_hand):
match list(processed_hand.values()):
case [5]:
return 6
case [4, 1]:
return 5
case [3, 2]:
return 4
case [3, 1, 1]:
return 3
case [2, 2, 1]:
return 2
case [2, 1, 1, 1]:
return 1
case _:
return 0
def compare_hands(hand_1, hand_2):
cards_order = '23456789TJQKA'
hand_1 = hand_1[0]
hand_2 = hand_2[0]
hand_1_value = calculate_hand_value(process_hand(hand_1))
hand_2_value = calculate_hand_value(process_hand(hand_2))
if hand_1_value > hand_2_value:
return 1
if hand_1_value < hand_2_value:
return -1
for card_hand_1, card_hand_2 in zip(hand_1, hand_2):
if card_hand_1 == card_hand_2:
continue
return 1 if cards_order.index(card_hand_1) > cards_order.index(card_hand_2) else -1
return 0
def calculate_total_winnings(sorted_data):
return sum((index + 1) * bid for index, (_, bid) in enumerate(sorted_data))
if __name__ == '__main__':
data = read_data('../input/07.txt')
# data = read_data('../test_input/07.txt') # 6440
data.sort(key=functools.cmp_to_key(compare_hands))
total_winnings = calculate_total_winnings(data)
print(total_winnings)