-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06b.py
More file actions
31 lines (21 loc) · 672 Bytes
/
day06b.py
File metadata and controls
31 lines (21 loc) · 672 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from functools import reduce
f = open('input.txt', 'r')
items = f.read().split('\n')
rows = items[:-1]
ops = items[-1]
idxs = [i for i in range(len(ops)) if ops[i] != ' '] + [len(ops)+1]
res = 0
for i in range(len(idxs)-1):
op = ops[idxs[i]]
nums = [[] for _ in range(idxs[i+1]-idxs[i]-1)]
for j in range(len(nums)):
for row in rows:
nums[j].append(row[idxs[i]+j])
nums = [''.join(list(filter(lambda a: a != ' ', num))) for num in nums]
nums = [int(num) for num in nums if num != '']
if op == '+':
res += sum(nums)
if op == '*':
res += reduce(lambda a,b: a*b, nums)
# print(nums)
print(res)