diff options
Diffstat (limited to '2025/06')
| -rw-r--r-- | 2025/06/a.py | 16 | ||||
| -rw-r--r-- | 2025/06/b.py | 32 |
2 files changed, 47 insertions, 1 deletions
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 @@ | |||
| 1 | import fileinput | 1 | import fileinput |
| 2 | from functools import reduce | ||
| 3 | from operator import mul | ||
| 2 | 4 | ||
| 5 | a = [] | ||
| 6 | op = [] | ||
| 3 | with fileinput.input() as lines: | 7 | with fileinput.input() as lines: |
| 4 | for line in lines: | 8 | for line in lines: |
| 5 | ... | 9 | t = line[:-1].split(' ') |
| 10 | if t[0] == '+' or t[0] == '*': | ||
| 11 | op = [x for x in t if x != ''] | ||
| 12 | else: | ||
| 13 | a.append([int(x) for x in t if x != '']) | ||
| 6 | 14 | ||
| 15 | def sol(a, op, i): | ||
| 16 | return (sum(a[j][i] for j in range(len(a))) if op[i] == '+' | ||
| 17 | else reduce(mul, (a[j][i] for j in range(len(a))), 1)) | ||
| 18 | |||
| 19 | |||
| 20 | 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 @@ | |||
| 1 | import fileinput | ||
| 2 | from functools import reduce | ||
| 3 | from operator import mul | ||
| 4 | |||
| 5 | with fileinput.input() as lines: | ||
| 6 | all = list(lines) | ||
| 7 | a = [line[:-1] for line in all[:-1]] | ||
| 8 | op = all[-1][:-1] | ||
| 9 | |||
| 10 | def sol(a, j, k): | ||
| 11 | nums = [] | ||
| 12 | i = k-2 | ||
| 13 | while i >= j: | ||
| 14 | n = 0 | ||
| 15 | for r in a: | ||
| 16 | if r[i] != ' ': | ||
| 17 | n = 10*n + int(r[i]) | ||
| 18 | nums.append(n) | ||
| 19 | i -= 1 | ||
| 20 | return sum(nums) if op[j] == '+' else reduce(mul, nums, 1) | ||
| 21 | |||
| 22 | j = 0 | ||
| 23 | s = 0 | ||
| 24 | while j < len(op): | ||
| 25 | k = j+1 | ||
| 26 | while k < len(op) and op[k] == ' ': | ||
| 27 | k += 1 | ||
| 28 | if k >= len(op): | ||
| 29 | k = max(len(r) for r in a)+1 | ||
| 30 | s += sol(a, j, k) | ||
| 31 | j = k | ||
| 32 | print(s) | ||
