day 7, saved by Counter
This commit is contained in:
42
d07/part2.py
Normal file
42
d07/part2.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import sys
|
||||
from collections import Counter as C
|
||||
from functools import cmp_to_key as ctk
|
||||
|
||||
H = list(reversed('AKQT98765432J'))
|
||||
|
||||
def score(h:str) -> int:
|
||||
D = C(h)
|
||||
j = D['J']
|
||||
del D['J']
|
||||
if j<5:
|
||||
m = max(D.values())
|
||||
for k,v in D.items():
|
||||
if v == m:
|
||||
D[k] = v+j
|
||||
break
|
||||
E = C(v for k,v in D.items()) if j<5 else {5:1}
|
||||
r = 0
|
||||
if 5 in E: # five of a kind
|
||||
r = 7
|
||||
elif 4 in E: # four of a kind
|
||||
r = 6
|
||||
elif 3 in E and 2 in E: # full house
|
||||
r = 5
|
||||
elif 3 in E: # three of a kind
|
||||
r = 4
|
||||
elif 2 in E and E[2] == 2: # two pairs
|
||||
r = 3
|
||||
elif 2 in E: # one pair
|
||||
r = 2
|
||||
else: # high card
|
||||
r = 1
|
||||
return (r, *[H.index(c) for c in h])
|
||||
|
||||
L = [ l.split() for l in sys.stdin.read().splitlines()]
|
||||
L = [(score(h),int(b)) for [h,b] in L]
|
||||
|
||||
sorted(L)
|
||||
|
||||
print(
|
||||
sum((i+1)*b for i,(h,b) in enumerate(reversed(L)))
|
||||
)
|
Reference in New Issue
Block a user