-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_18.py
87 lines (73 loc) · 2.52 KB
/
12_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def evaluate(expr):
expr = expr.strip()
stack = []
cval = None
expr = expr.replace("(", "( ").replace(")", " )")
tokens = expr.split()
operator = None
for token in tokens:
if token.isdigit():
if cval is None:
cval = int(token)
continue
if operator == "*":
cval *= int(token)
else:
cval += int(token)
elif token == "(":
stack.append((cval, operator))
cval = None
elif token == ")":
nc, nop = stack.pop()
if nop == "*":
cval = (nc if nc is not None else 1) * cval
else:
cval = (nc if nc is not None else 0) + cval
else:
operator = token
return cval
def day18_1():
exprs = [x for x in open("num18.txt").read().split("\n") if x]
return sum(evaluate(x) for x in exprs)
def evaluate2(expr): # shunting-yard algorithm http://www.martinbroadhurst.com/shunting-yard-algorithm-in-python.html
ops = {"*": lambda x, y: int(x) * int(y), "+": lambda x, y: int(x) + int(y)}
prec = {"+": 1, "*": 0}
expr = expr.strip()
stack = []
output = []
expr = expr.replace("(", "( ").replace(")", " )")
tokens = expr.split()
for token in tokens:
if token.isdigit():
output.append(int(token))
elif token == '(':
stack.append(token)
elif token == ')':
top = stack[-1] if stack else None
while top is not None and top != '(':
operator = stack.pop()
right = output.pop()
left = output.pop()
output.append(ops[operator](left, right))
top = stack[-1] if stack else None
stack.pop()
else:
top = stack[-1] if stack else None
while top is not None and top not in "()" and prec.get(top) > prec.get(token):
operator = stack.pop()
right = output.pop()
left = output.pop()
output.append(ops[operator](left, right))
top = stack[-1] if stack else None
stack.append(token)
while stack:
operator = stack.pop()
right = output.pop()
left = output.pop()
output.append(ops[operator](left, right))
return output[0]
def day18_2():
exprs = [x for x in open("num18.txt").read().split("\n") if x]
return sum(evaluate2(x) for x in exprs)
print(day18_1())
print(day18_2())