-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18.py
52 lines (42 loc) · 1.15 KB
/
18.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
48
49
50
51
52
import re
def reverse_polish_notation(line, prec):
stack = list()
output = ''
for c in line:
if c == ' ':
continue
if re.match('\d', c):
output += c
elif c == '(':
stack.append(c)
elif c == ')':
while stack[-1] != '(':
output += stack.pop()
stack.pop()
else:
while stack and stack[-1] != '(' and prec[stack[-1]] >= prec[c]:
output += stack.pop()
stack.append(c)
while stack:
output += stack.pop()
return output
def calc(line, prec):
line = reverse_polish_notation(line, prec)
stack = list()
for c in line:
if re.match('\d', c):
stack.append(int(c))
else:
a, b = stack.pop(), stack.pop()
stack.append(eval(f"{a} {c} {b}"))
return stack.pop()
def star1():
prec = {'*': 1, '+': 1}
print(sum(calc(line.strip(), prec) for line in open('input.txt')))
def star2():
prec = {'*': 1, '+': 2}
print(sum(calc(line.strip(), prec) for line in open('input.txt')))
print('Star 1:')
star1()
print('Star 2:')
star2()