-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc6_part2.py
More file actions
59 lines (46 loc) · 1.62 KB
/
aoc6_part2.py
File metadata and controls
59 lines (46 loc) · 1.62 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import operator
from itertools import accumulate
import math
if __name__ == '__main__':
f = open('./data/input6.txt')
lines = f.readlines()
operators = lines[-1].split()
digits_1 = lines[0].split()
digits_2 = lines[1].split()
digits_3 = lines[2].split()
digits_4 = lines[3].split()
widths = []
num_space = 1
for c in reversed(lines[-1]):
if c in ('+', '*'):
widths.append(num_space)
num_space = 1
else:
num_space += 1
#widths.append(0)
widths = widths[::-1]
accumulated_widths = [0] + list(accumulate(widths, operator.add))
accumulated_widths = accumulated_widths[:-1]
#print(widths)
#print(accumulated_widths)
operator = ''
grand_total = 0
for j in range(len(accumulated_widths)):
nums = []
for i in range(widths[j]):
num = ''
for line in lines:
#print(line[i+4])
if line[i+accumulated_widths[j]] not in (' ', '*', '+'):
num += line[i+accumulated_widths[j]]
elif line[i+accumulated_widths[j]] in ('*', '+'):
operator = line[i+accumulated_widths[j]]
nums.append(num)
# print(nums)
total = 0
if operator == '+':
total = sum([int(x) for x in nums if x != ''])
elif operator == '*':
total = math.prod([int(x) for x in nums if x != ''])
grand_total += total
print(grand_total)