day 7, saved by Counter
This commit is contained in:
18
d07/terse.py
Normal file
18
d07/terse.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from collections import Counter
|
||||
lines = [i for i in open('input').read().split('\n') if i.strip()]
|
||||
def hand(h,part1):
|
||||
if part1: h = h.replace('J', 'X')
|
||||
h2 = ['J23456789TXQKA'.index(i)for i in h]
|
||||
ts = []
|
||||
for r in '23456789TQKA':
|
||||
c = Counter(h.replace('J', r))
|
||||
p = tuple(sorted(c.values()))
|
||||
t = [(1,1,1,1,1),(1,1,1,2),(1,2,2),(1,1,3),(2,3),(1,4),(5,)].index(p)
|
||||
ts.append(t)
|
||||
return (max(ts), *h2)
|
||||
for part1 in (True, False):
|
||||
h = sorted((hand(h,part1), int(b)) for h, b in (l.split() for l in lines))
|
||||
t = 0
|
||||
for i,(_,b) in enumerate(h):
|
||||
t+=i*b+b
|
||||
print('Part', 2-part1, ':', t)
|
Reference in New Issue
Block a user