From 4a070e827b3d671be892ea9d3fd18989b28dcef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Wed, 3 Dec 2025 07:30:04 +0100 Subject: [PATCH] Solve 2025 day 3 pt 1-2 For the first part, I used itertools.combinations to find the highest pairs of batteries. And as expected, that solution did not scale well for pt 2. I figured out that reducing batteries until the top most 12 (and 2) remained was the correct way to go. the _maxj(line, C) function is the hive conclusion from the solution mega thread. I really liked this brilliant use of a while loop to exlude batteries. - The first char just skip the while loop. A char emptying the battery list also does this. --- 2025-python/output/day_03.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2025-python/output/day_03.py diff --git a/2025-python/output/day_03.py b/2025-python/output/day_03.py new file mode 100644 index 0000000..8d993b3 --- /dev/null +++ b/2025-python/output/day_03.py @@ -0,0 +1,31 @@ +def solve(data): + p1 = 0 + p2 = 0 + for line in data.splitlines(): + p1 += _maxj(line, 2) + p2 += _maxj(line, 12) + return p1, p2 + + +def _maxj(line, C): + toexcl = len(line) - C + batt = [] + for c in line: + while toexcl and batt and batt[-1] < c: + toexcl -= 1 + batt.pop() + batt.append(c) + return sum(10**x * int(y) for x, y in zip(range(C - 1, -1, -1), batt)) + + +if __name__ == "__main__": + with open("./input/03.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 17430 + assert p2 == 171975854269367