-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.py
37 lines (32 loc) · 1.07 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
class MyNumA:
def __init__(self, x): self.x = x
def __add__(self, o): return MyNumA(self.x + o.x)
def __sub__(self, o): return MyNumA(self.x * o.x)
@staticmethod
def repl(line):
return line.replace("*", "-")
class MyNumB:
def __init__(self, x): self.x = x
def __mul__(self, o): return MyNumB(self.x + o.x)
def __sub__(self, o): return MyNumB(self.x * o.x)
@staticmethod
def repl(line):
return line.replace("*", "-").replace("+", "*")
def compute(line, num_class):
line = num_class.repl(line)
to_eval = []
i = 0
while i < len(line):
if line[i].isdigit():
end = i + 1
while end < len(line) and line[end].isdigit():
end += 1
to_eval.append(f"{num_class.__name__}({line[i:end]})")
i = end
else:
to_eval.append(line[i])
i += 1
return eval("".join(to_eval)).x
lines = open("inputs/day18.txt").read().splitlines()
print(sum(map(lambda l: compute(l, MyNumA), lines)))
print(sum(map(lambda l: compute(l, MyNumB), lines)))