-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18-1.py
More file actions
40 lines (34 loc) · 1014 Bytes
/
18-1.py
File metadata and controls
40 lines (34 loc) · 1014 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
38
39
40
def evaluate(s):
last_num = 0
op = "+"
i = 0
while i < len(s):
if s[i] == "+" or s[i] == "*": # it's an operation
op = s[i]
else:
if s[i] == "(":
stack = 1
for j in range(i+1, len(s)):
if s[j] == "(":
stack += 1
elif s[j] == ")":
stack -= 1
if stack == 0:
curr_num = evaluate(s[i+1:j])
i = j
break
else: #it's a number
curr_num = int(s[i])
if op == "+":
last_num += curr_num
elif op == "*":
last_num *= curr_num
i += 1
return last_num
total_sum = 0
with open("18.in", "r") as file:
for l in file:
l = l.strip().replace(" ", "")
total_sum += evaluate(l)
print(total_sum)
#print(evaluate("1+(2*3)+(4*(5+6))"))