-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18-1.py
47 lines (44 loc) · 1.09 KB
/
18-1.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
47
rawdata = open("18-1.input.txt").readlines()
depth=0
def solve(sum,depth):
ans=0
i = 0
op = "+"
num=0
while i<len(sum):
if sum[i]=="+":
op="+"
elif sum[i]=="*":
op="*"
elif sum[i]=="(":
sub=sum[i+1:]
numtoskip=0
pairpos=0
for y,c in enumerate(sub):
if c=="(":
numtoskip+=1
if c==")":
numtoskip+=-1
if numtoskip==-1:
pairpos=y
break
recurse=sub[:pairpos]
if op=="+":
ans+=solve(recurse,depth+1)
elif op=="*":
ans*=solve(recurse,depth+1)
i+=len(recurse)
else: # num
if sum[i]!=")":
num=int(sum[i])
if op=="+":
ans+=num
elif op=="*":
ans*=num
i+=1
return ans
depth=0
ans=0
for line in rawdata:
ans+=solve(line.strip().replace(" ",""),depth)
print("ans=",ans)