-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06.py
More file actions
38 lines (33 loc) · 979 Bytes
/
day06.py
File metadata and controls
38 lines (33 loc) · 979 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
32
33
34
35
36
37
import operator
import functools
def main():
file = open('data/day06.txt', 'r')
lines = []
lines2 = []
for line in file.readlines():
if line[0] == '+' or line[0] == '*':
operators = [operator.mul if x == '*' else operator.add for x in line.split()]
else:
lines.append([int(x) for x in line.split()])
lines2.append(line)
res = 0
for i in range(len(lines[0])):
res += functools.reduce(operators[i], [line[i] for line in lines])
print(res)
j = len(operators) - 1
arr = []
res = 0
for i in range(len(lines2[0])-1, -2, -1):
num = 0
for line in lines2:
if (line[i].isnumeric()):
num = num*10 + int(line[i])
if num != 0:
arr.append(num)
else:
if len(arr) > 0:
res += functools.reduce(operators[j], arr)
arr = []
j -= 1
print(res)
main()