59 lines
1.3 KiB
Python
59 lines
1.3 KiB
Python
import fileinput
|
|
from collections import Counter
|
|
|
|
STRENGTHS = {
|
|
"A": 1,
|
|
"K": 2,
|
|
"Q": 3,
|
|
"T": 5,
|
|
"9": 6,
|
|
"8": 7,
|
|
"7": 8,
|
|
"6": 9,
|
|
"5": 10,
|
|
"4": 11,
|
|
"3": 12,
|
|
"2": 13,
|
|
}
|
|
|
|
INPUT = []
|
|
for line in fileinput.input():
|
|
(hand, bid) = line.split()
|
|
counter = Counter(hand)
|
|
INPUT.append((hand, int(bid), counter["J"], counter.most_common() + [("", 0)]))
|
|
|
|
def solve(part):
|
|
STRENGTHS["J"] = 14 if part == 2 else 4
|
|
|
|
hands = []
|
|
for (hand, bid, jokers, mcs) in INPUT:
|
|
if part == 2:
|
|
mcs = [(c, i) for (c, i) in mcs if c != "J"]
|
|
else:
|
|
jokers = 0
|
|
mc = mcs[0][1] + jokers
|
|
|
|
if mc == 5:
|
|
strength = 1
|
|
elif mc == 4:
|
|
strength = 2
|
|
elif mc == 3 and mcs[1][1] == 2:
|
|
strength = 3
|
|
elif mc == 3:
|
|
strength = 4
|
|
elif mc == 2 and mcs[1][1] == 2:
|
|
strength = 5
|
|
elif mc == 2:
|
|
strength = 6
|
|
else:
|
|
strength = 7
|
|
|
|
strengths = [STRENGTHS[c] for c in hand]
|
|
hands.append(([strength] + strengths, bid))
|
|
|
|
hands.sort(key=lambda h: h[0], reverse=True)
|
|
return sum((i + 1) * bid for (i, (_, bid)) in enumerate(hands))
|
|
|
|
part1 = solve(part=1)
|
|
part2 = solve(part=2)
|
|
print(part1, part2)
|