day 4 part 2

main
magical 2023-12-04 08:34:33 +00:00
parent 33171e5512
commit 1ee568b507
1 changed files with 16 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import sys
from collections import defaultdict
def matches2score(n):
if n <= 0:
@ -7,15 +8,30 @@ def matches2score(n):
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)