defexecute_program(lines):lines_executed=set()cursor=0accumulator=0defacc(lines,cursor,accumulator):return(cursor+1,accumulator+int(lines[cursor][1]))defjmp(lines,cursor,accumulator):return(cursor+int(lines[cursor][1]),accumulator)defnop(lines,cursor,accumulator):return(cursor+1,accumulator)terminated=Falsewhilenotterminatedandcursornotinlines_executed:instruction=lines[cursor][0]lines_executed.add(cursor)operations={'jmp':jmp,'acc':acc,'nop':nop,}operation=operations[instruction]cursor,accumulator=operation(lines,cursor,accumulator)# Terminate if end at program
terminated=cursor==len(lines)returnterminated,accumulatordefpart1(lines):_,result=execute_program(lines)returnresultprintpart1(lines)defpart2(lines):foriinrange(len(lines)):# Copy lines so that changes don't persist.
local_lines=[xforxinlines]# Switch statement jmp/nop
if'jmp'inlocal_lines[i][0]:local_lines[i]=('nop',local_lines[i][1])elif'nop'inlocal_lines[i][0]:local_lines[i]=('jmp',local_lines[i][1])terminated,result=execute_program(local_lines)ifterminated:breakreturnresultprint"Solution part 2: %d"%part2(lines)
My solution for day 08, where I have tried to make it easy to extend. Just in case we have to continue building on this another day 😬 (getting some intcode flashbacks from 2019...)
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.
Python
My solution for day 08, where I have tried to make it easy to extend. Just in case we have to continue building on this another day 😬 (getting some intcode flashbacks from 2019...)