Part 2 of this one stumped me. I'm posting my code for part 1 here so I don't feel like a total failure, but I'm pretty bummed. I knew that you had to manually step through the instructions or convert them to some sort of higher level thing so that you could see what they were trying to do slowly and shortcut it. But, when I went through all that work and came up with a wrong answer, I looked here. I was thinking that we were summing all of the numbers up until a certain large number, but I was on the wrong track. After looking here at the answer, it seemed like cheating to finish a solution and get the star.
That's one point for you, Advent.
Anyways, here's my code for part 1 in Python:
"""Day 19: Go with the flow
Figure out control flow with time machine opcodes.
from day16_ops import NAMED_OPERATIONS
def execute(instructions, ip_register, initial_registers=None):
"""Executes a bunch of instructions against 6 registers"""
ip = 0
if initial_registers is None:
registers = [0, 0, 0, 0, 0, 0]
registers = initial_registers[:]
while 0 <= ip < len(instructions):
op, a, b, c = instructions[ip]
registers[ip_register] = ip
registers = op(registers, a, b, c)
ip = registers[ip_register]
ip += 1
"""Parses a text file of instructions into actual instructions"""
lines = text.splitlines()
ip_register = int(lines[-1])
instructions = 
for line in lines[1:]:
op_name, a, b, c = line.split()
instructions.append([NAMED_OPERATIONS[op_name], int(a), int(b), int(c)])
return ip_register, instructions
if __name__ == "__main__":
# Part 1
with open("python/data/day19.txt", "r") as f:
puzzle_input = f.read()
ip_register, instructions = parse_instructions(puzzle_input)
results = execute(instructions, ip_register)
print("Value of register 0: ", results)
# Part 2
# results = execute(instructions, ip_register, [1, 0, 0, 0, 0, 0])
# print("New value of register 0: ", results)
# TOO SLOW
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.