Day 5 required some additions to my Intcode computer - namely jumping and comparison operators. The puzzle also required me to implement support for parameter and immediate modes depending on the values left of the raw opcode.
Let’s get to it! As always, you can find my full solutions repo here.
The first step in extending capability is to support parameter and immediate modes for arguments. I addressed this by using a fixed size list and padding the extra space with 0’s (as the instructions outline). This fits all cases nicely since missing mode digits indicate a 0 and opcodes that won’t use the extra 0’s aren’t bothered by their inclusion in the data structure.
def get_param_modes(value): if value < 100: return [0, 0, 0] value = value // 100 param_modes = [int(i) for i in str(value)] while len(param_modes) < 3: param_modes.insert(0, 0) return param_modes
I’m taking a similar one-size-fits-all approach to the argument situation. By using the mode information, I can prep the arguments so that the opcode portion of the code doesn’t have to worry about this detail.
def parse_args(program, opcode, pc, param_modes): args = [0, 0, 0] # Opcodes w/ 1 arg if opcode == 3: args = program[pc+1] elif opcode == 4: if param_modes == 0: args = program[program[pc+1]] else: args = program[pc+1] # Opcodes w/ 2 or 3 args elif opcode in (1, 2, 5, 6, 7, 8): args = program[program[pc+1]] if param_modes == 0 else program[pc+1] args = program[program[pc+2]] if param_modes == 0 else program[pc+2] # Opcodes w/ 3 args if opcode in (1, 2, 7, 8): args = program[pc+3] return args
Implementing the new opcodes are pretty straightforward, although some of the
finer points require carefully reviewing the spec. Right now, I have my
as a large
if-elif-else chain but it’s starting to getunpleasant to look at.
If I hit another puzzle that requires Intcode, I’ll refactor the opcode sections
into individual functions. I’m not sure how that’ll look yet, but that’s a
problem for the future. (Is this…tech debt?)
Onward to day 6 (on December 10th…)!
As always, you can find my full solutions repo here.