You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

23 lines
543 B

import ast
class RewriteName(ast.NodeTransformer):
def visit_Mult(self, node):
return ast.Add()
def visit_Sub(self, node):
return ast.Mult()
def evaluate(line):
line = line.replace("*", "-")[:-1]
line = line.replace("+", "*")
new = RewriteName().visit(ast.parse(line, mode='eval'))
return eval(compile(new, '<string>', 'eval'))
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input18") as f:
lines = f.readlines()
print(evaluate(lines[0]))
print(sum([evaluate(l) for l in lines]))