day 23
opening the turing lock
approach
each line is split into a list of tokens, and the single-register operations and the jump tests live in a dict of lambdas. execute runs a program counter over the instructions with structural pattern matching, one case per instruction shape.
part 1 starts with both registers at zero and reads b. part 2 starts a at one.
the trick
match on the shape of the instruction and look the operation up in a table.
python
def execute(regs, instructions):
pc = 0
while pc < len(instructions):
match instructions[pc]:
case ["hlf" | "tpl" | "inc" as ins, reg]:
regs[reg] = funcs[ins](regs[reg])
pc += 1
case ["jmp", offset]:
pc += int(offset)
case ["jie" | "jio" as ins, reg, offset]:
pc += int(offset) if funcs[ins](regs[reg]) else 1