Skip to content

Instantly share code, notes, and snippets.

@sciyoshi
Created December 18, 2020 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sciyoshi/2475c0cf5fd74ebaf8aa9e84923c2c4a to your computer and use it in GitHub Desktop.
Save sciyoshi/2475c0cf5fd74ebaf8aa9e84923c2c4a to your computer and use it in GitHub Desktop.
class a(int):
def __mul__(self, b):
return a(int(self) + b)
def __add__(self, b):
return a(int(self) + b)
def __sub__(self, b):
return a(int(self) * b)
def ev(expr, pt2=False):
expr = re.sub(r"(\d+)", r"a(\1)", expr)
expr = expr.replace("*", "-")
if pt2:
expr = expr.replace("+", "*")
return eval(expr, {}, {"a": a})
print("Part 1:", sum(ev(l) for l in LINES))
print("Part 2:", sum(ev(l, pt2=True) for l in LINES))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment