Advertisement
hhoppe

Advent of code 2020 day 18 too compact

Dec 18th, 2020 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. def compute(s, part2=False):
  2.  
  3.   def eval_term(i):
  4.     return eval_seq(i + 1, eat=1) if s[i] == '(' else (int(s[i]), i + 1)
  5.  
  6.   def eval_seq(i, eat=0):
  7.     value, i = eval_term(i)
  8.     while i < len(s) and s[i] != ')':
  9.       is_mul = s[i + 1] == '*'
  10.       value2, i = (eval_seq if part2 and is_mul else eval_term)(i + 3)
  11.       value = value * value2 if is_mul else value + value2
  12.     return value, i + eat
  13.  
  14.   return eval_seq(0)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement