-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
53 lines (41 loc) · 1.34 KB
/
day18.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
from util import Day
from aocd import submit
def eval_parens(expression, evaluate):
while "(" in expression:
for k in expression.split("("):
if ")" in k:
innter = k.split(")")[0]
expression = expression.replace("(" + innter + ")", str(evaluate(innter)))
return expression
def eval_left(expression):
expression = expression.split()
while ("*" in expression) or ("+" in expression):
expression = [str(eval("".join(expression[:3])))] + expression[3:]
return int(expression[0])
def eval_precedence(expression):
expression = expression.split()
while "+" in expression:
plus = expression.index("+")
expression[plus] = str(eval("".join(expression[plus - 1 : plus + 2])))
expression.pop(plus + 1)
expression.pop(plus - 1)
return eval("".join(expression))
def main(day, part=1):
if part == 1:
evaluate = eval_left
if part == 2:
evaluate = eval_precedence
day.apply(eval_parens, evaluate=evaluate)
day.apply(evaluate)
return sum(day.data)
if __name__ == "__main__":
day = Day(18)
day.download()
day.load(typing=str)
p1 = main(day)
print(p1)
submit(p1, part="a", day=18, year=2020)
day.load(typing=str)
p2 = main(day, part=2)
print(p2)
submit(p2, part="b", day=18, year=2020)