33 lines
816 B
Python
Executable File
33 lines
816 B
Python
Executable File
#!/usr/bin/env python3
|
|
import re, sys
|
|
|
|
class ltr:
|
|
def __init__(self, n):
|
|
self.val = n
|
|
def __add__(self, x):
|
|
return ltr(self.val + x.val)
|
|
def __sub__(self, x):
|
|
return ltr(self.val * x.val)
|
|
|
|
class flip:
|
|
def __init__(self, n):
|
|
self.val = n
|
|
def __truediv__(self, x):
|
|
return flip(self.val + x.val)
|
|
def __sub__(self, x):
|
|
return flip(self.val * x.val)
|
|
|
|
def toltr(l):
|
|
return re.sub(r'(\d)', r'ltr(\1)', l.replace('*', '-'))
|
|
|
|
def toflip(l):
|
|
return re.sub(r'(\d)', r'flip(\1)', l.replace('*', '-').replace('+', '/'))
|
|
|
|
fn = sys.argv[1] if len(sys.argv) > 1 else 'input%s' % 18
|
|
with open(fn) as f:
|
|
|
|
lines = [l.strip() for l in f.readlines()]
|
|
|
|
print(sum(eval(toltr(l)).val for l in lines))
|
|
print(sum(eval(toflip(l)).val for l in lines))
|