-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.py
46 lines (36 loc) · 1.02 KB
/
part1.py
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
def myeval(string, i, j):
result = None
operation = None
buff = ""
while i < j:
if string[i] in ('+', '*'):
if result is None:
result = int(buff)
elif operation == '+':
result += int(buff)
else:
result *= int(buff)
operation = string[i]
buff = ""
elif string[i] == '(':
k = i
opened = 1
while opened > 0:
i += 1
opened += 1 if string[i] == '(' else -1 if string[i] == ')' else 0
buff = myeval(string, k + 1, i)
else:
buff += string[i]
i += 1
if buff:
if result is None:
result = int(buff)
elif operation == '+':
result += int(buff)
else:
result *= int(buff)
return result
with open("input.txt") as file:
print(sum(
myeval(line, 0, len(line)) for line in (line.strip().replace(' ', '') for line in file)
))