|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools,copy |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | + |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.findall(r"[a-zA-Z]+",s) |
| 31 | + |
| 32 | + |
| 33 | +test_data= {} |
| 34 | + |
| 35 | +test=1 |
| 36 | +test_data[test]= { |
| 37 | +"input":"""nop +0 |
| 38 | +acc +1 |
| 39 | +jmp +4 |
| 40 | +acc +3 |
| 41 | +jmp -3 |
| 42 | +acc -99 |
| 43 | +acc +1 |
| 44 | +jmp -4 |
| 45 | +acc +6""", |
| 46 | +"expected": ["5","8"], |
| 47 | +} |
| 48 | + |
| 49 | +test="real" |
| 50 | +input_file=os.path.join( |
| 51 | +os.path.dirname(__file__), |
| 52 | +"Inputs", |
| 53 | +os.path.basename(__file__).replace(".py",".txt"), |
| 54 | +) |
| 55 | +test_data[test]= { |
| 56 | +"input":open(input_file,"r+").read(), |
| 57 | +"expected": ["1134","1205"], |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +# -------------------------------- Control program execution ------------------------- # |
| 62 | + |
| 63 | +case_to_test=1 |
| 64 | +part_to_test=2 |
| 65 | + |
| 66 | +# -------------------------------- Initialize some variables ------------------------- # |
| 67 | + |
| 68 | +puzzle_input=test_data[case_to_test]["input"] |
| 69 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 70 | +puzzle_actual_result="Unknown" |
| 71 | + |
| 72 | + |
| 73 | +# -------------------------------- Actual code execution ----------------------------- # |
| 74 | + |
| 75 | + |
| 76 | +classProgram: |
| 77 | +def__init__(self,instructions): |
| 78 | +self.instructions= [ |
| 79 | + [x.split(" ")[0],int(x.split(" ")[1])]forxininstructions.split("\n") |
| 80 | + ] |
| 81 | +self.accumulator=0 |
| 82 | +self.current_line=0 |
| 83 | +self.operations= { |
| 84 | +"nop":self.nop, |
| 85 | +"acc":self.acc, |
| 86 | +"jmp":self.jmp, |
| 87 | + } |
| 88 | + |
| 89 | +defrun(self): |
| 90 | +whilecurrent_line<=len(self.operations): |
| 91 | +self.run_once() |
| 92 | + |
| 93 | +defrun_once(self): |
| 94 | +instr=self.instructions[self.current_line] |
| 95 | +print("Before",self.current_line,self.accumulator,instr) |
| 96 | +self.operations[instr[0]](instr) |
| 97 | + |
| 98 | +defnop(self,instr): |
| 99 | +self.current_line+=1 |
| 100 | +pass |
| 101 | + |
| 102 | +defacc(self,instr): |
| 103 | +self.current_line+=1 |
| 104 | +self.accumulator+=instr[1] |
| 105 | + |
| 106 | +defjmp(self,instr): |
| 107 | +self.current_line+=instr[1] |
| 108 | + |
| 109 | + |
| 110 | +ifpart_to_test==1: |
| 111 | +program=Program(puzzle_input) |
| 112 | + |
| 113 | +visited= [] |
| 114 | +while ( |
| 115 | +program.current_line<len(program.instructions) |
| 116 | +andprogram.current_linenotinvisited |
| 117 | + ): |
| 118 | +visited.append(program.current_line) |
| 119 | +program.run_once() |
| 120 | + |
| 121 | +puzzle_actual_result=program.accumulator |
| 122 | + |
| 123 | + |
| 124 | +else: |
| 125 | +initial_program=Program(puzzle_input) |
| 126 | +all_nop_jmp= [ |
| 127 | +i |
| 128 | +fori,instrinenumerate(initial_program.instructions) |
| 129 | +ifinstr[0]in ("jmp","nop") |
| 130 | + ] |
| 131 | +forvalinall_nop_jmp: |
| 132 | +program=copy.deepcopy(initial_program) |
| 133 | +program.instructions[val][0]= ( |
| 134 | +"nop"ifprogram.instructions[val][0]=="jpm"else"nop" |
| 135 | + ) |
| 136 | + |
| 137 | +visited= [] |
| 138 | +while ( |
| 139 | +program.current_line<len(program.instructions) |
| 140 | +andprogram.current_linenotinvisited |
| 141 | + ): |
| 142 | +visited.append(program.current_line) |
| 143 | +program.run_once() |
| 144 | + |
| 145 | +ifprogram.current_line==len(program.instructions): |
| 146 | +puzzle_actual_result=program.accumulator |
| 147 | +break |
| 148 | + |
| 149 | + |
| 150 | +# -------------------------------- Outputs / results --------------------------------- # |
| 151 | + |
| 152 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 153 | +print("Expected result : "+str(puzzle_expected_result)) |
| 154 | +print("Actual result : "+str(puzzle_actual_result)) |