aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit be683b101393d3566261c9cc10351398bd2e1b6c
parent 424ffd61c58eb127f9a657a854737e2d82368217
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sat,  6 Dec 2025 07:57:16 +0100

Day 6 2025 I suck

Diffstat:
M2025/06/a.py | 16+++++++++++++++-
A2025/06/b.py | 32++++++++++++++++++++++++++++++++
M2025/README.md | 16++++++++++++++++
MREADME.md | 2+-
4 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/2025/06/a.py b/2025/06/a.py @@ -1,6 +1,20 @@ import fileinput +from functools import reduce +from operator import mul +a = [] +op = [] with fileinput.input() as lines: for line in lines: - ... + t = line[:-1].split(' ') + if t[0] == '+' or t[0] == '*': + op = [x for x in t if x != ''] + else: + a.append([int(x) for x in t if x != '']) +def sol(a, op, i): + return (sum(a[j][i] for j in range(len(a))) if op[i] == '+' + else reduce(mul, (a[j][i] for j in range(len(a))), 1)) + + +print(sum(sol(a, op, i) for i in range(len(a[0])))) diff --git a/2025/06/b.py b/2025/06/b.py @@ -0,0 +1,32 @@ +import fileinput +from functools import reduce +from operator import mul + +with fileinput.input() as lines: + all = list(lines) + a = [line[:-1] for line in all[:-1]] + op = all[-1][:-1] + +def sol(a, j, k): + nums = [] + i = k-2 + while i >= j: + n = 0 + for r in a: + if r[i] != ' ': + n = 10*n + int(r[i]) + nums.append(n) + i -= 1 + return sum(nums) if op[j] == '+' else reduce(mul, nums, 1) + +j = 0 +s = 0 +while j < len(op): + k = j+1 + while k < len(op) and op[k] == ' ': + k += 1 + if k >= len(op): + k = max(len(r) for r in a)+1 + s += sol(a, j, k) + j = k +print(s) diff --git a/2025/README.md b/2025/README.md @@ -11,6 +11,7 @@ Example ``` Day -Part 1- -Part 2- + 6 00:13:38 01:49:24 5 00:04:39 00:22:19 4 00:25:36 00:27:49 3 00:05:02 00:16:09 @@ -69,3 +70,18 @@ Part 2 required a little bit of thinking to handle overlaps correctly - at first I wrote a solution that did not handle overlaps, then one that can only handle single overlaps, and finally one that works in every case. My final solution is quite straightforward. + +### Day 6: Trash Compactor + +Part 1 was easy, but I struggled with part 2. + +I had to solve this while travelling, which did not help, but that was +not the main issue. The problem was that for whatever reason my script +did not copy the whitespaces correctly from the sample input in the web +page, so I was left wondering how the heck am I supposed to align the +numbers. After more than an hour and after changing train, I figured out +the error and I was able to solve this elementary school problem. Much +smart, very accomplishment. + +This year so far the only problems that took me more than 30 minutes +are this and the first one, not exactly the hardest problems imaginable. diff --git a/README.md b/README.md @@ -14,4 +14,4 @@ See `year/README.md` for instructions on how to run my code. |2022| 50 | Rust | Done in 2025 to learn Rust | |2023| 50 | C | All solved by December 25, 2023 | |2024| 50 | C++ | Each solved within 24h | -|2025| 10 | Python | Work in progress... | +|2025| 12 | Python | Work in progress... |