advent2023/four.py

32 lines
624 B
Python
Executable File

#!/usr/bin/env python3
from useful import *
ls = lines(open(0)) # pretty much every time, yeah
def munge(l):
x = l.split(':')[1].strip()
return [set(numbers(s)) for s in x.split('|')]
win_draw = [munge(l) for l in ls]
def wins(w, d):
return len(w & d)
def score(n):
if n > 0:
return 2**(n-1)
return 0
print(sum(score(wins(*wd)) for wd in win_draw))
def boost(cs, n, p):
return [(c + n * (i < p), s) for i, (c, s) in enumerate(cs)]
def score2(cs):
if not cs:
return 0
return cs[0][0] + score2(boost(cs[1:], *cs[0]))
print(score2([(1, wins(*wd)) for wd in win_draw]))