-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.py
164 lines (125 loc) · 4.49 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Description: Day 7 Part 2 of Advent of Code
CARD_STRENGTH = {
'A': 0,
'K': 1,
'Q': 2,
'T': 3,
'9': 4,
'8': 5,
'7': 6,
'6': 7,
'5': 8,
'4': 9,
'3': 10,
'2': 11,
'J': 12
}
def strongest_card(hand: str) -> str:
"""Returns the strongest card in a hand"""
strongest_card = ""
strongest_value = 13
for card in hand:
value = CARD_STRENGTH.get(card)
if value < strongest_value :
strongest_card = card
strongest_value = value
return strongest_card
def compare_hands(hand1: str, hand2: str) -> int:
"""compare two hands and return 1 if hand 1 is stronger and return 2 if hand 2 is stronger and 0 if they are equals"""
occurences_hand1 = {}
occurences_hand2 = {}
for card_index in range(len(hand1)):
card1 = hand1[card_index]
card2 = hand2[card_index]
if card1 not in occurences_hand1:
occurences_hand1[card1] = 0
if card2 not in occurences_hand2:
occurences_hand2[card2] = 0
occurences_hand1[card1] += 1
occurences_hand2[card2] += 1
if 'J' in occurences_hand1:
occ_joker_hand1 = occurences_hand1['J']
if occ_joker_hand1 == 5:
del occurences_hand1['J']
occurences_hand1['A'] = 5
else:
del occurences_hand1['J']
max_occ_hand1 = max(occurences_hand1.items(), key=lambda x: x[1])
if max_occ_hand1[1] == 1:
strong_card_label = strongest_card(hand1)
max_occ_hand1 = (strong_card_label, 1)
tmp_label, tmp_int = max_occ_hand1
del occurences_hand1[tmp_label]
occurences_hand1[tmp_label] = tmp_int + occ_joker_hand1
if 'J' in occurences_hand2:
occ_joker_hand2 = occurences_hand2['J']
if occ_joker_hand2 == 5:
del occurences_hand2['J']
occurences_hand2['A'] = 5
else:
del occurences_hand2['J']
max_occ_hand2 = max(occurences_hand2.items(), key=lambda x: x[1])
if max_occ_hand2[1] == 1:
strong_card_label = strongest_card(hand2)
max_occ_hand2 = (strong_card_label, 1)
tmp_label, tmp_int = max_occ_hand2
del occurences_hand2[tmp_label]
occurences_hand2[tmp_label] = tmp_int + occ_joker_hand2
occurences_hand1 = list(occurences_hand1.items())
occurences_hand1.sort(key=lambda x: x[1], reverse=True)
max_occ_hand1 = occurences_hand1[0][1]
if len(occurences_hand1) == 1:
sec_max_occ_hand1 = max_occ_hand1
else:
sec_max_occ_hand1 = occurences_hand1[1][1]
occurences_hand2 = list(occurences_hand2.items())
occurences_hand2.sort(key=lambda x: x[1], reverse=True)
max_occ_hand2 = occurences_hand2[0][1]
if len(occurences_hand2) == 1:
sec_max_occ_hand2 = max_occ_hand2
else:
sec_max_occ_hand2 = occurences_hand2[1][1]
if max_occ_hand1 > max_occ_hand2:
return 1
elif max_occ_hand2 > max_occ_hand1:
return 2
else:
if sec_max_occ_hand1 > sec_max_occ_hand2:
return 1
elif sec_max_occ_hand2 > sec_max_occ_hand1:
return 2
else:
for card_index in range(len(hand1)):
card1 = hand1[card_index]
card2 = hand2[card_index]
if CARD_STRENGTH.get(card1) < CARD_STRENGTH.get(card2):
return 1
elif CARD_STRENGTH.get(card2) < CARD_STRENGTH.get(card1):
return 2
else:
continue
return 0
def solution(textfile: str) -> int:
"""Returns the total winnings"""
# Artifact due to change of the template
lines = textfile.splitlines()
lines = [line+"\n" for line in lines]
lines[-1] = lines[-1][:-1]
# End of artifact
hands = []
for line in lines:
current_hand, current_bid = line.split()
if len(hands) == 0:
hands.append((current_hand, current_bid))
continue
index = 0
hand = hands[index][0]
while index < len(hands) and compare_hands(current_hand, hand) == 2:
index += 1
if index < len(hands):
hand = hands[index][0]
hands.insert(index, (current_hand, current_bid))
bids = 0
for index_hand, hand in enumerate(hands):
bids += int(hand[1]) * (len(hands) - index_hand)
return bids