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 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 2025/06/b.py (limited to '2025/06') 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) -- cgit v1.3