adventofcode2023/day04/sol.py

38 lines
727 B
Python
Raw Normal View History

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