Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
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.
"""fromday16_opsimportNAMED_OPERATIONSdefexecute(instructions,ip_register,initial_registers=None):"""Executes a bunch of instructions against 6 registers"""ip=0ifinitial_registersisNone:registers=[0,0,0,0,0,0]else:registers=initial_registers[:]while0<=ip<len(instructions):op,a,b,c=instructions[ip]registers[ip_register]=ipregisters=op(registers,a,b,c)ip=registers[ip_register]ip+=1returnregistersdefparse_instructions(text):"""Parses a text file of instructions into actual instructions"""lines=text.splitlines()ip_register=int(lines[0][-1])instructions=[]forlineinlines[1:]:op_name,a,b,c=line.split()instructions.append([NAMED_OPERATIONS[op_name],int(a),int(b),int(c)])returnip_register,instructionsif__name__=="__main__":# Part 1
withopen("python/data/day19.txt","r")asf:puzzle_input=f.read()ip_register,instructions=parse_instructions(puzzle_input)results=execute(instructions,ip_register)print("Value of register 0: ",results[0])# Part 2
# results = execute(instructions, ip_register, [1, 0, 0, 0, 0, 0])
# print("New value of register 0: ", results[0])
# TOO SLOW
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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: