Day 8 may be the first real trouble I have had so far. Part 1 wasn’t too bad, but Part 2 has had me stuck for a bit.
The job is to troubleshoot bad code for a handheld device. It gets stuck in an infinite loop. The input is a string of commands that either increment an accumulator, or jump forward or back in the code. Sometimes there is no action.
Part 1 was to execute the code without ever repeating a command.
with open('day8data.txt') as f:
lines = [line.rstrip() for line in f]
accumulator=0
location=0
commands_executed=[]
while(location<=len(lines)):
command = lines[location][:3]
amount = int(lines[location][4:])
#print command
#print amount
if command == 'acc':
accumulator+=amount
location+=1
if command == 'jmp':
location+=amount
if command == 'nop':
location+=1
if location not in commands_executed:
commands_executed.append(location)
else:
break
#print commands_executed
print accumulator
Basically, run each command, tracking which have been executed, then, when you reach a repeat, which will cause a loop, you exit.
Part 2 had me for a bit, but mostly because I missed what it was looking for. The object is to change one of the jump or no operation commands, to cause the loop to break and the code to complete. My original approach and thought was, that at some point in the loop, the code would swoop down to the bottom of the commands, and then loop away. So I was checking for if the code was close to the end, then changing the jump or no-operation there.
What I needed to be doing was changing each one, and then testing it through the solve loop to see if it would complete or not (no loops).
Basically, run the code, anytime there is a jump or no-op, flip it and check for a loop. If not, keep going on the original path.
def solver(accumulator, location, commands_executed, switcher):
while(True):
if lines[location] == "":
print accumulator
return False
command = lines[location][:3]
amount = int(lines[location][4:])
#print command
#print amount
if switcher==1 and command == 'jmp':
switcher = 0
command = 'nop'
if switcher==1 and command == 'nop':
switcher = 0
command = 'jmp'
if command == 'acc':
accumulator+=amount
location+=1
if command == 'jmp':
location+=amount
if command == 'nop':
location+=1
if location not in commands_executed:
commands_executed.append(location)
else:
break
#print commands_executed
return True
with open('day8data.txt') as f:
lines = [line.rstrip() for line in f]
accumulator=0
location=0
breaker = True
commands_executed=[]
while(breaker):
while(True):
command = lines[location][:3]
amount = int(lines[location][4:])
#print command
#print amount
if command == "":
print accumulator
if command == 'acc':
accumulator+=amount
location+=1
if command == 'jmp':
breaker = solver(accumulator,location, commands_executed, 1)
location+=amount
if command == 'nop':
breaker = solver(accumulator,location, commands_executed, 1)
location+=1
if location not in commands_executed:
commands_executed.append(location)
else:
break
#print commands_executed
I tried to get some recursion going here but I couldn’t work it out so I just did it with repeated code blocks. Because this is why I dislike recursion. If I need to pass around and check for changing conditionals, I may as well put them in a regular loop.
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.