-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_18.py
94 lines (68 loc) · 1.99 KB
/
day_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
88
89
90
91
92
93
94
import aoc_helper
raw = aoc_helper.fetch(18, year=2020)
# print(raw)
# raw = """1 + 2 * 3 + 4 * 5 + 6"""
def parse_raw():
return [i for i in raw.splitlines()]
data = parse_raw()
def evaluate_expression(expression: str):
total = 0
token = ""
operation = ""
in_brackets = 0
for char in expression + " ":
if char == " " and not in_brackets:
# print(total, operation, token)
if token.isnumeric():
if operation == "":
total = int(token)
elif operation == "+":
total += int(token)
elif operation == "*":
total *= int(token)
else:
operation = token
token = ""
elif char == "(":
in_brackets += 1
token += "("
elif char == ")":
token += ")"
in_brackets -= 1
if not in_brackets:
token = str(evaluate_expression(token[1:-1]))
else:
token += char
return total
def part_one():
return sum(evaluate_expression(line) for line in data)
import rply
lg = rply.LexerGenerator()
lg.add("NUM", r"\d+")
lg.add("ADD", r"\+")
lg.add("MUL", r"\*")
lg.add("LPAREN", r"\(")
lg.add("RPAREN", r"\)")
lg.ignore("\s+")
lexer = lg.build()
pg = rply.ParserGenerator(
["NUM", "ADD", "MUL", "LPAREN", "RPAREN"], [("left", ["MUL"]), ("left", ["ADD"])]
)
@pg.production("expr : NUM")
def _(tokens):
return int(tokens[0].value)
@pg.production("expr : expr ADD expr")
def _(tokens):
return tokens[0] + tokens[2]
@pg.production("expr : expr MUL expr")
def _(tokens):
return tokens[0] * tokens[2]
@pg.production("expr : LPAREN expr RPAREN")
def _(tokens):
return tokens[1]
parser = pg.build()
def part_two():
return sum(parser.parse(lexer.lex(line)) for line in data)
# print(part_one())
aoc_helper.lazy_submit(day=18, year=2020, solution=part_one)
aoc_helper.lazy_submit(day=18, year=2020, solution=part_two)