From be683b101393d3566261c9cc10351398bd2e1b6c Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sat, 6 Dec 2025 07:57:16 +0100 Subject: Day 6 2025 I suck --- 2025/06/a.py | 16 +++++++++++++++- 2025/06/b.py | 32 ++++++++++++++++++++++++++++++++ 2025/README.md | 16 ++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 2025/06/b.py (limited to '2025') diff --git a/2025/06/a.py b/2025/06/a.py index 5fdb11b..1d12e01 100644 --- 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 new file mode 100644 index 0000000..d52560f --- /dev/null +++ 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 index a77c168..548deb3 100644 --- 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. -- cgit v1.3