|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,pathfinding,IntCode |
| 3 | + |
| 4 | +fromcomplex_utilsimport* |
| 5 | + |
| 6 | +test_data= {} |
| 7 | + |
| 8 | +test="real" |
| 9 | +input_file=os.path.join( |
| 10 | +os.path.dirname(__file__), |
| 11 | +"Inputs", |
| 12 | +os.path.basename(__file__).replace(".py",".txt"), |
| 13 | +) |
| 14 | +test_data[test]= { |
| 15 | +"input":open(input_file,"r+").read().strip(), |
| 16 | +"expected": ["5068","1415975"], |
| 17 | +} |
| 18 | + |
| 19 | +# -------------------------------- Control program execution ------------------------- # |
| 20 | + |
| 21 | +case_to_test="real" |
| 22 | +part_to_test=2 |
| 23 | +verbose_level=0 |
| 24 | + |
| 25 | +# -------------------------------- Initialize some variables ------------------------- # |
| 26 | + |
| 27 | +puzzle_input=test_data[case_to_test]["input"] |
| 28 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 29 | +puzzle_actual_result="Unknown" |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------- Actual code execution ----------------------------- # |
| 33 | + |
| 34 | +position=0 |
| 35 | + |
| 36 | +droid=IntCode.IntCode(puzzle_input) |
| 37 | +droid.run() |
| 38 | +grid= [] |
| 39 | +foroutputindroid.outputs: |
| 40 | +ifchr(output)=="#": |
| 41 | +grid.append(position) |
| 42 | +elifchr(output)in ["^","v",">","<"]: |
| 43 | +droid_pos= [position,accent_to_dir[chr(output)]] |
| 44 | + |
| 45 | +ifchr(output)=="\n": |
| 46 | +position=j* (position.imag-1) |
| 47 | +else: |
| 48 | +position+=1 |
| 49 | + |
| 50 | + |
| 51 | +ifpart_to_test==1: |
| 52 | +alignment_parameter=0 |
| 53 | +forxinrange(1,int(max_real(grid))): |
| 54 | +foryinrange(int(min_imag(grid)),-1): |
| 55 | +ifx+y*jingrid: |
| 56 | +ifall([x+y*j+diringridfordirindirections_straight]): |
| 57 | +alignment_parameter+=x*-y |
| 58 | + |
| 59 | +puzzle_actual_result=alignment_parameter |
| 60 | + |
| 61 | + |
| 62 | +else: |
| 63 | +steps= [] |
| 64 | +visited= [] |
| 65 | + |
| 66 | +# Find the path, in the long form (L,12,R,8,.....) |
| 67 | +whileTrue: |
| 68 | +position,direction=droid_pos |
| 69 | +visited.append(position) |
| 70 | +ifposition+directioningrid: |
| 71 | +steps[-1]+=1 |
| 72 | +droid_pos[0]+=droid_pos[1] |
| 73 | +else: |
| 74 | +option= [ |
| 75 | + (turn[0].upper(),direction*relative_directions[turn]) |
| 76 | +forturninrelative_directions |
| 77 | +ifposition+direction*relative_directions[turn]ingrid |
| 78 | +ifposition+direction*relative_directions[turn]notinvisited |
| 79 | + ] |
| 80 | +iflen(option)>1: |
| 81 | +print("error") |
| 82 | +raiseException(position,direction,option) |
| 83 | + |
| 84 | +ifoption: |
| 85 | +option=option[0] |
| 86 | +steps+= [option[0],1] |
| 87 | +droid_pos[1]=option[1] |
| 88 | +droid_pos[0]+=droid_pos[1] |
| 89 | +else: |
| 90 | +break |
| 91 | + |
| 92 | +steps=list(map(str,steps)) |
| 93 | +steps_inline=",".join(steps) |
| 94 | + |
| 95 | +# Shorten the path |
| 96 | +subprograms= [] |
| 97 | +nb_to_letter= {0:"A",1:"B",2:"C"} |
| 98 | + |
| 99 | +offset=0 |
| 100 | +foriinrange(3): |
| 101 | +whilelen(subprograms)==i: |
| 102 | +nb_steps=min(20,len(steps)-offset) |
| 103 | +subprogram=steps[offset :offset+nb_steps] |
| 104 | +subprogram_inline=",".join(subprogram) |
| 105 | + |
| 106 | +# The limits of 3 is arbitrary |
| 107 | +while ( |
| 108 | +steps_inline.count(subprogram_inline)<3orlen(subprogram_inline)>20 |
| 109 | + ): |
| 110 | +# Shorten subprogram for test |
| 111 | +iflen(subprogram)<=2: |
| 112 | +break |
| 113 | +else: |
| 114 | +ifsubprogram[-1]in ("A","B","C"): |
| 115 | +delsubprogram[-1] |
| 116 | +else: |
| 117 | +delsubprogram[-2:] |
| 118 | + |
| 119 | +subprogram_inline=",".join(subprogram) |
| 120 | + |
| 121 | +# Found one! |
| 122 | +ifsteps_inline.count(subprogram_inline)>=3andlen(subprogram)>2: |
| 123 | +subprograms.append(subprogram_inline) |
| 124 | +steps_inline=steps_inline.replace(subprogram_inline,nb_to_letter[i]) |
| 125 | +steps=steps_inline.split(",") |
| 126 | +else: |
| 127 | +ifsteps[offset]in ["A","B","C"]: |
| 128 | +offset+=1 |
| 129 | +else: |
| 130 | +offset+=2 |
| 131 | +offset=0 |
| 132 | + |
| 133 | +# Now send all that to the robot |
| 134 | +droid.instructions[0]=2 |
| 135 | +inputs= ( |
| 136 | +steps_inline+"\n"+"\n".join(subprograms)+"\nn\n" |
| 137 | + )# the last n is for the video |
| 138 | +forletterininputs: |
| 139 | +droid.add_input(ord(letter)) |
| 140 | +droid.restart() |
| 141 | +droid.run() |
| 142 | + |
| 143 | +puzzle_actual_result=droid.outputs.pop() |
| 144 | +ifverbose_level: |
| 145 | +foroutputindroid.outputs: |
| 146 | +print(chr(output),end="") |
| 147 | + |
| 148 | + |
| 149 | +# -------------------------------- Outputs / results --------------------------------- # |
| 150 | + |
| 151 | +print("Expected result : "+str(puzzle_expected_result)) |
| 152 | +print("Actual result : "+str(puzzle_actual_result)) |