-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day7_1.py
executable file
·69 lines (55 loc) · 1.58 KB
/
aoc23_day7_1.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
#!/usr/bin/env python
class Hand(object):
OPTIONS = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
def __init__(self, hand):
self.hand = hand
@staticmethod
def score(hand):
counts = {}
for c in hand:
counts.setdefault(c, 0)
counts[c] += 1
count_counts = {}
for count in counts.values():
count_counts.setdefault(count, 0)
count_counts[count] += 1
if count_counts.get(5, 0) == 1:
return 7
elif count_counts.get(4, 0) == 1:
return 6
elif count_counts.get(3, 0) == 1:
if count_counts.get(2, 0) == 1:
return 5
else:
return 4
elif count_counts.get(2, 0) == 2:
return 3
elif count_counts.get(2, 0) == 1:
return 2
else:
return 1
def __lt__(self, other):
key1 = [self.score(self.hand)] + \
[self.OPTIONS.index(c) for c in self.hand]
key2 = [self.score(other.hand)] + \
[self.OPTIONS.index(c) for c in other.hand]
return key1 < key2
def read_input():
res = []
while True:
try:
line = input()
hand, bid = line.split()
res.append((Hand(hand), int(bid)))
except EOFError:
break
return res
def main():
hands_bids = read_input()
hands_bids.sort()
res = 0
for i, hand_bid in enumerate(hands_bids):
res += (i + 1) * hand_bid[1]
print(res)
if __name__ == '__main__':
main()