From cb622409f9ca686f642eead27857dbd5e67dbed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 3 Dec 2024 06:20:25 +0100 Subject: [PATCH] Solve 2024:3 p1-2 "Mull It Over" Felt paranoid on this one, was expecting something much worse for pt2. The code that solved the puzzle was _extremely_ naive: - It asumes puzzle input only contains mul() with 1-3 chars. The versioned code have `{1,3}` as safe guard. - p2 asumed initial chunk does not begin with "n't". Would have been easy to handle though. - p2 asumed junk strings as "don", "do", "don()t" would not exist. In other words, I was really lucky since I did not look that closely on puzzle input beforehand. Might have cut 60-90 seconds further if I had just ran pt2 immidately instead of staring dumbly on the test data (that changed for pt2). Also, I got reminded that \d in regular expressions is equal to `0-9`: it does not include commas and punctations. The code that solved the puzzle was paranoid and instead used `0-9`. Managed to score ~1500th somehow despite this. --- 2024-python/aoc.py | 2 +- 2024-python/output/__init__.py | 1 + 2024-python/output/day_03.py | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 2024-python/output/day_03.py diff --git a/2024-python/aoc.py b/2024-python/aoc.py index 5040efa..c84d089 100644 --- a/2024-python/aoc.py +++ b/2024-python/aoc.py @@ -34,7 +34,7 @@ from collections import deque, Counter from heapq import heappop, heappush from itertools import compress, combinations, chain -from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg +from output import matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg def solve(data): diff --git a/2024-python/output/__init__.py b/2024-python/output/__init__.py index d1d71d4..7e7bab1 100644 --- a/2024-python/output/__init__.py +++ b/2024-python/output/__init__.py @@ -127,6 +127,7 @@ def dijkstras(grid, start, target): all nodes. """ import heapq + target = max(grid) seen = set() queue = [(start, 0)] diff --git a/2024-python/output/day_03.py b/2024-python/output/day_03.py new file mode 100644 index 0000000..ee20ca8 --- /dev/null +++ b/2024-python/output/day_03.py @@ -0,0 +1,26 @@ +import re +from math import prod + + +def solve(data): + needle = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)") + + p1 = sum(prod(map(int, factors)) for factors in re.findall(needle, data)) + + p2 = sum( + sum(prod(map(int, factors)) for factors in re.findall(needle, chunk)) + for chunk in data.split("do") + if not chunk.startswith("n't") + ) + + return p1, p2 + + +if __name__ == "__main__": + with open("./input/03.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)