This was much more enjoyable after the utter faff that was day 7
defboot(bootcode):acc=0visited=set()pos=0whileTrue:ifpos>=len(bootcode):return(acc,True)ifposinvisited:return(acc,False)op,n=bootcode[pos].split(' ')n=int(n)visited.add(pos)ifop=="nop":pos+=1elifop=="acc":acc+=npos+=1elifop=="jmp":pos+=ndefswap_op(line):op,n=line.split(' ')ifop=="nop":op="jmp"elifop=="jmp":op="nop"# leave acc unaffected
return" ".join((op,n))#return "jmp" if op == "nop" else "nop"
deffix(bootcode):val,terminated=boot(bootcode)ifterminated:returnval,terminatedfori,linenumerate(bootcode):# don't modify the original
code_copy=[lforlinbootcode]code_copy[i]=swap_op(l)val,terminated=boot(code_copy)ifterminated:returnval,terminated#bootcode[i] = swap_op(l)
returnval,Falsebootcode=[r.strip()forrinopen('input.txt')]acc,term=boot(bootcode)print(f"Part 1: the original bootcode terminates {''iftermelse'ab'}normally with accumulator value {acc}")acc,term=fix(bootcode)print(f"Part 2: the fixed bootcode terminates {''iftermelse'ab'}normally with accumulator value {acc}")
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.
This was much more enjoyable after the utter faff that was day 7