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