Skip to content
advent of code 2019

day 05

sunny with a chance of asteroids

approach

the intcode interpreter grows parameter modes, input/output, jumps and comparisons. parse pads the instruction to five digits and splits it into the opcode and one mode character per parameter; param reads either the cell a parameter points at (mode 0) or the parameter itself (mode 1). arithmetic and comparisons live in a binop dict, the two jumps in a unop dict.

exec is a generator: opcode 3 stores the single input value and opcode 4 yields an output. jumps set ip directly, which is why their entry in oplen is 0. part 1 runs with input 1 and part 2 with input 5; both return the first non-zero output.

the trick

making the interpreter a generator turns output into yield, so the caller just iterates.

python
def part1(ic=intcode[:]):
	for i in exec(ic,1):
		if i!=0:
			return i