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
|
#!/bin/python3
import sys
from math import sqrt
output = 0
CARD_MAP = {
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"T": 10,
"J": 11,
"Q": 12,
"K": 13,
"A": 14,
}
FIVE_OF_A_KIND = 7
FOUR_OF_A_KIND = 6
FULL_HOUSE = 5
THREE_OF_A_KIND = 4
TWO_PAIR = 3
ONE_PAIR = 2
HIGH_CARD = 1
hands = []
for line in sys.stdin:
line = line.strip()
cards, bid = line.split()
bid = int(bid)
# sort_by is a list of values to sort the final hands list by
sort_by = []
tally = {}
for card in cards:
code = CARD_MAP[card]
tally[code] = tally.get(code, 0) + 1
sort_by.append(code)
# first index in sort_by is the type of hand
hand_type = 0
if 5 in tally.values():
hand_type = FIVE_OF_A_KIND
elif 4 in tally.values():
hand_type = FOUR_OF_A_KIND
elif 3 in tally.values() and 2 in tally.values():
hand_type = FULL_HOUSE
elif 3 in tally.values() and 2 == len([v for v in tally.values() if v == 1]):
hand_type = THREE_OF_A_KIND
elif 2 == len([v for v in tally.values() if v == 2]):
hand_type = TWO_PAIR
elif 1 == len([v for v in tally.values() if v == 2]):
hand_type = ONE_PAIR
elif 5 == len([v for v in tally.values() if v == 1]):
hand_type = HIGH_CARD
else:
hand_type = 0
sort_by.insert(0, hand_type)
hand = {
"bid": bid,
"sort": sort_by,
}
hands.append(hand)
# print(cards, bid, tally)
hands.sort(key=lambda h: h["sort"])
output = sum(hand["bid"] * (index + 1) for index, hand in enumerate(hands))
print(f">> {output}")
|