diff --git a/day04/sol.py b/day04/sol.py index 0d1e768..29f284e 100644 --- a/day04/sol.py +++ b/day04/sol.py @@ -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)