adventofcode2023/day04/sol.py

38 lines
727 B
Python

import sys
from collections import defaultdict
def matches2score(n):
if n <= 0:
return 0
return 2**(n-1)
input = sys.stdin
part1 = 0
card = 0
copies = defaultdict(int)
for line in input:
card += 1
_, _, data = line.partition(": ")
winners, numbers = data.split("|")
winners = map(int, winners.split())
numbers = map(int, numbers.split())
matches = len(set(winners) & set(numbers))
s = matches2score(matches)
print(s)
part1 += s
copies[card] += 1 # original copy
for i in range(matches):
copies[card+i+1] += copies[card]
print("-")
print(part1)
assert not [i for i,c in copies.items() if c > 0 and i > card]
part2 = sum(copies.values())
print(part2)