@@ -13,28 +13,78 @@ def run(self):
1313 instruction  =  instructions [self .instruction_pointer ]
1414 op_code  =  instruction .split (" " )[0 ]
1515
16+  self .instruction_pointer  +=  1 
17+ 
1618 if  op_code  ==  "nop" :
17-  print ( "hehe" ) 
19+  pass 
1820 elif  op_code  ==  "jmp" :
1921 self .instruction_pointer  +=  int (instruction .split (" " )[1 ]) -  1 
2022 elif  op_code  ==  "acc" :
2123 self .accumulator  +=  int (instruction .split (" " )[1 ])
2224 else :
2325 print ("invalid instruction" )
2426
25-  self .instruction_pointer  +=  1 
27+  def  run_all (self ):
28+  while  self .instruction_pointer  <  len (instructions ):
29+  self .run ()
30+ 
31+  def  terminates (self ) ->  bool :
32+  executed_lines  =  set ()
33+ 
34+  while  self .instruction_pointer  not  in   executed_lines :
35+  executed_lines .add (program .instruction_pointer )
36+ 
37+  if  self .instruction_pointer  >=  len (self .instructions ):
38+  return  True 
39+ 
40+  self .run ()
41+ 
42+  return  False 
2643
2744
2845instructions  =  read_lines ("day08" )
2946program  =  Program (instructions )
3047
3148# Part one 
32- executed_lines   =   set ()
49+ program . terminates ()
3350
34- while  program .instruction_pointer  not  in   executed_lines :
35-  executed_lines .add (program .instruction_pointer )
36-  program .run ()
51+ aoc_print (f"The program gets interrupted with an accumulator of { program .accumulator }  ." )
52+ assert_equals (1801 , program .accumulator )
3753
38- print (program .accumulator )
3954
4055# Part two 
56+ def  replace_all (base_list : [str ], old : str , new : str ) ->  [[str ]]:
57+  total  =  list ()
58+  index  =  0 
59+ 
60+  while  True :
61+  tup  =  replace (list (base_list ), old , new , index )
62+  if  tup [1 ] ==  - 1 :
63+  break 
64+ 
65+  total .append (tup [0 ])
66+  index  =  tup [1 ]
67+ 
68+  return  total 
69+ 
70+ 
71+ def  replace (replacement_list : [str ], old : str , new : str , start : int ) ->  ([str ], int ):
72+  for  i  in  range (start , len (replacement_list )):
73+  if  old  in  replacement_list [i ]:
74+  replacement_list [i ] =  replacement_list [i ].replace (old , new )
75+  return  replacement_list , i  +  1 
76+ 
77+  return  [], - 1 
78+ 
79+ 
80+ replaced_instructions_list  =  replace_all (read_lines ("day08" ), "jmp" , "nop" ) \
81+  +  replace_all (read_lines ("day08" ), "nop" , "jmp" )
82+ 
83+ for  instructions  in  replaced_instructions_list :
84+  program  =  Program (instructions )
85+  if  program .terminates ():
86+  program  =  Program (instructions )
87+  program .run_all ()
88+ 
89+  aoc_print (f"The program terminates with an accumulator of { program .accumulator }  ." )
90+  assert_equals (2060 , program .accumulator )
0 commit comments