day 08
i heard you like registers
approach
each instruction is split by a regex into a register, an inc/dec that becomes a small lambda, and the condition text after if. the condition is already valid python, so it's handed to eval with a defaultdict(int) of registers as the local namespace, which makes unknown registers read as 0.
the run keeps the register dict and the highest value ever written. part 1 is the largest register at the end; part 2 is that running maximum.
the trick
the conditions are python expressions as written, so eval against the registers does all the parsing.
python
def execute(instructions):
context = defaultdict(int)
mx = 0
for var, op, cond in instructions:
if eval(cond, None, context):
context[var] = op(context[var])
mx = max(mx, context[var])
return context, mx