From 1ee568b5074b6931dc3db2ba475093ef3a037acb Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Mon, 4 Dec 2023 08:34:33 +0000 Subject: [PATCH] day 4 part 2 --- day04/sol.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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)