-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_18.py
77 lines (61 loc) · 2.51 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
#%% Part1
# Before using this you have to run `antlr4 -Dlanguage=Python3 -no-listener -visitor Day18Part1.g4`
from antlr4 import InputStream, CommonTokenStream, ParseTreeVisitor
from Day18Part1Lexer import Day18Part1Lexer
from Day18Part1Parser import Day18Part1Parser
from Day18Part1Visitor import Day18Part1Visitor
from Day18Part2Lexer import Day18Part2Lexer
from Day18Part2Parser import Day18Part2Parser
from Day18Part2Visitor import Day18Part2Visitor
class Day18Part1ExprVisitor(Day18Part1Visitor):
def visitOpExpr(self, ctx: Day18Part1Parser.OpExprContext):
left = self.visit(ctx.expr(0))
right = self.visit(ctx.expr(1))
op = ctx.op.text
if op == "+":
return left + right
elif op == "-":
return left - right
elif op == "*":
return left * right
def visitAtomExpr(self, ctx: Day18Part1Parser.AtomExprContext):
return int(ctx.getText())
def visitBraceExpr(self, ctx: Day18Part1Parser.BraceExprContext):
return self.visit(ctx.expr())
lexer = Day18Part1Lexer()
parser = Day18Part1Parser(CommonTokenStream(lexer))
visitor = Day18Part1ExprVisitor()
with open("day_18_input.txt") as input_data:
result = []
for line in input_data:
lexer.inputStream = InputStream(line.strip())
parser.setTokenStream(CommonTokenStream(lexer))
result.append(visitor.visit(parser.expr()))
print(f"Sum of all results: {sum(result)}")
#%%
# Before using this you have to run `antlr4 -Dlanguage=Python3 -no-listener -visitor Day18Part2.g4`
class Day18Part2ExprVisitor(Day18Part2Visitor):
def visitOpExpr(self, ctx: Day18Part2Parser.OpExprContext):
left = self.visit(ctx.expr(0))
right = self.visit(ctx.expr(1))
op = ctx.op.text
if op == "+":
return left + right
elif op == "-":
return left - right
elif op == "*":
return left * right
def visitAtomExpr(self, ctx: Day18Part2Parser.AtomExprContext):
return int(ctx.getText())
def visitBraceExpr(self, ctx: Day18Part2Parser.BraceExprContext):
return self.visit(ctx.expr())
lexer = Day18Part2Lexer()
parser = Day18Part2Parser(CommonTokenStream(lexer))
visitor = Day18Part2ExprVisitor()
with open("day_18_input.txt") as input_data:
result = []
for line in input_data:
lexer.inputStream = InputStream(line.strip())
parser.setTokenStream(CommonTokenStream(lexer))
result.append(visitor.visit(parser.expr()))
print(f"Sum of all results: {sum(result)}")