From cbd4bf50e184e92a4374d82f027e3521850eb282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Mon, 4 Dec 2023 07:27:47 +0100 Subject: [PATCH] Solve 2023:04 "Scratchcards" On a train that according to swedish tradition was late. Not a good environment to focus. Got stuck 2 times: - Initial code asumed the | was always after the 5th number, because of the example. Puzzle input had it at pos 10. Classic AoC mistake. - I had a hard time trying to understand the score count, I insisted there was meant to be a +1 at some point. > That means card 1 is worth 8 points (1 for > the first match, then doubled three times for > each of the three matches after the first) I should instead have just looked at the numbers. --- 2023-python/output/day_04.py | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2023-python/output/day_04.py diff --git a/2023-python/output/day_04.py b/2023-python/output/day_04.py new file mode 100644 index 0000000..40afde8 --- /dev/null +++ b/2023-python/output/day_04.py @@ -0,0 +1,50 @@ +import re +from collections import defaultdict +from output import answer, puzzleinput + +n = 4 +title = "Scratchcards" + + +@answer(1, "Sum of all scratchcard points are {}") +def part_1(presolved): + scores, _ = presolved + return scores + + +@answer(2, "Ends up wih a total of {} scratchcards") +def part_2(presolved): + _, count = presolved + return count + + +@puzzleinput(n) +def parse_input(data): + return data + + +def presolve(data): + scores = [] + count = defaultdict(int) + for cid, line in enumerate(data.splitlines()): + a, b = line.split("|") + a = set(re.findall(r"\d+", a)[1:]) + b = set(re.findall(r"\d+", b)) + ab = len(a & b) + if ab > 0: + scores.append(2**(ab - 1)) + count[cid] += 1 + for i in range(cid + 1, cid + ab + 1): + count[i] += count[cid] + return sum(scores), sum(count.values()) + + +if __name__ == "__main__": + inp = parse_input() + inp = presolve(inp) + + a = part_1(inp) + b = part_2(inp) + + assert a == 21919 + assert b == 9881048