-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
36 lines (32 loc) · 942 Bytes
/
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
import re
class Number:
def __init__(self, value):
self.value = int(value)
def __add__(self, other):
return Number(self.value + other.value)
def __sub__(self, other):
return Number(self.value * other.value)
def __mul__(self, other):
return Number(self.value + other.value)
def __str__(self):
return str(self.value)
def run(lines, part):
sum = 0
for line in lines:
line = line.replace('*', '-')
if part == 2:
line = line.replace('+', '*')
expr = re.sub(r'(\d+)', r'Number(\1)', line)
result = eval(expr)
sum += result.value
return sum
def main(inp):
with open(inp) as f:
lines = f.read().splitlines()
res1 = run(lines, 1)
res2 = run(lines, 2)
return res1, res2
if __name__ == '__main__':
res1, res2 = main('../input/input18.txt')
print('day18.1:', res1)
print('day18.2:', res2)