day 02
1202 program alarm
approach
the first intcode day. the program is a list of ints, and exec walks it four cells at a time: read the opcode, look up + or * in a dict of lambdas, apply it to the two cells the parameters point at, and write to the third. opcode 99 stops the loop. before running, exec writes the noun and verb into positions 1 and 2.
part 1 runs a copy with noun 12 and verb 2 and reads position 0. part 2 tries every noun and verb from 0 to 99 on a fresh copy until position 0 holds the target value, and returns the (noun, verb) pair; combining them into the final number is left to you.
the trick
every instruction has the same shape, so the interpreter is one line inside a loop.
python
def exec(ic,noun,verb):
ic[1]=noun
ic[2]=verb
cur=0
while (oc:=ic[cur])!=99:
ic[ic[cur+3]]=ops[oc](ic[ic[cur+1]],ic[ic[cur+2]])
cur+=4
return ic