|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,pathfinding |
| 3 | + |
| 4 | +fromcomplex_utilsimport* |
| 5 | + |
| 6 | +test_data= {} |
| 7 | + |
| 8 | +test=1 |
| 9 | +test_data[test]= { |
| 10 | +"input":"""....# |
| 11 | +#..#. |
| 12 | +#..## |
| 13 | +..#.. |
| 14 | +#....""", |
| 15 | +"expected": ["2129920","99"], |
| 16 | +} |
| 17 | + |
| 18 | +test="real" |
| 19 | +input_file=os.path.join( |
| 20 | +os.path.dirname(__file__), |
| 21 | +"Inputs", |
| 22 | +os.path.basename(__file__).replace(".py",".txt"), |
| 23 | +) |
| 24 | +test_data[test]= { |
| 25 | +"input":open(input_file,"r+").read(), |
| 26 | +"expected": ["20751345","1983"], |
| 27 | +} |
| 28 | + |
| 29 | +# -------------------------------- Control program execution ------------------------- # |
| 30 | + |
| 31 | +case_to_test="real" |
| 32 | +part_to_test=2 |
| 33 | + |
| 34 | +# -------------------------------- Initialize some variables ------------------------- # |
| 35 | + |
| 36 | +puzzle_input=test_data[case_to_test]["input"] |
| 37 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 38 | +puzzle_actual_result="Unknown" |
| 39 | + |
| 40 | + |
| 41 | +# -------------------------------- Actual code execution ----------------------------- # |
| 42 | + |
| 43 | + |
| 44 | +defgrid_to_vertices(self,grid): |
| 45 | +self.vertices= {} |
| 46 | +y=0 |
| 47 | +forlineingrid.splitlines(): |
| 48 | +forxinrange(len(line)): |
| 49 | +self.vertices[x-y*j]=line[x] |
| 50 | +y+=1 |
| 51 | + |
| 52 | +forsourceinself.vertices: |
| 53 | +fordirectionindirections_straight: |
| 54 | +target=source+direction |
| 55 | +iftargetinself.vertices: |
| 56 | +ifsourceinself.edges: |
| 57 | +self.edges[source].append(target) |
| 58 | +else: |
| 59 | +self.edges[source]= [target] |
| 60 | + |
| 61 | +returnTrue |
| 62 | + |
| 63 | + |
| 64 | +pathfinding.Graph.grid_to_vertices=grid_to_vertices |
| 65 | + |
| 66 | + |
| 67 | +defbiodiversity_rating(self): |
| 68 | +rating=0 |
| 69 | +foryinrange(int(min_imag(self.vertices)),int(max_imag(self.vertices)+1)): |
| 70 | +forxinrange(int(min_real(self.vertices)),int(max_real(self.vertices)+1)): |
| 71 | +ifself.vertices[x+y*j]=="#": |
| 72 | +rating+=pow(2,-y* (max_real(self.vertices)+1)+x) |
| 73 | + |
| 74 | +returnint(rating) |
| 75 | + |
| 76 | + |
| 77 | +pathfinding.Graph.biodiversity_rating=biodiversity_rating |
| 78 | + |
| 79 | + |
| 80 | +ifpart_to_test==1: |
| 81 | +empty_grid= ("."*5+"\n")*5 |
| 82 | +area=pathfinding.Graph() |
| 83 | +new_area=pathfinding.Graph() |
| 84 | +area.grid_to_vertices(puzzle_input) |
| 85 | + |
| 86 | +previous_ratings= [] |
| 87 | +whilearea.biodiversity_rating()notinprevious_ratings: |
| 88 | +previous_ratings.append(area.biodiversity_rating()) |
| 89 | +new_area.grid_to_vertices(empty_grid) |
| 90 | +forpositioninarea.vertices: |
| 91 | +ifarea.vertices[position]=="#": |
| 92 | +living_neighbors=len( |
| 93 | + [ |
| 94 | +neighbor |
| 95 | +forneighborinarea.neighbors(position) |
| 96 | +ifarea.vertices[neighbor]=="#" |
| 97 | + ] |
| 98 | + ) |
| 99 | +ifliving_neighbors==1: |
| 100 | +new_area.vertices[position]="#" |
| 101 | +else: |
| 102 | +new_area.vertices[position]="." |
| 103 | +else: |
| 104 | +living_neighbors=len( |
| 105 | + [ |
| 106 | +neighbor |
| 107 | +forneighborinarea.neighbors(position) |
| 108 | +ifarea.vertices[neighbor]=="#" |
| 109 | + ] |
| 110 | + ) |
| 111 | +ifliving_neighborsin (1,2): |
| 112 | +new_area.vertices[position]="#" |
| 113 | +else: |
| 114 | +new_area.vertices[position]="." |
| 115 | + |
| 116 | +area.vertices=new_area.vertices.copy() |
| 117 | + |
| 118 | +puzzle_actual_result=area.biodiversity_rating() |
| 119 | + |
| 120 | +else: |
| 121 | + |
| 122 | +defneighbors(self,vertex): |
| 123 | +neighbors= [] |
| 124 | +position,level=vertex |
| 125 | +fordirindirections_straight: |
| 126 | +if (position+dir,level)inself.vertices: |
| 127 | +neighbors.append((position+dir,level)) |
| 128 | + |
| 129 | +# Connection to lower (outside) levels |
| 130 | +ifposition.imag==0: |
| 131 | +neighbors.append((2-1*j,level-1)) |
| 132 | +elifposition.imag==-4: |
| 133 | +neighbors.append((2-3*j,level-1)) |
| 134 | +ifposition.real==0: |
| 135 | +neighbors.append((1-2*j,level-1)) |
| 136 | +elifposition.real==4: |
| 137 | +neighbors.append((3-2*j,level-1)) |
| 138 | + |
| 139 | +# Connection to higher (inside) levels |
| 140 | +ifposition==2-1*j: |
| 141 | +neighbors+= [(x,level+1)forxinrange(5)] |
| 142 | +elifposition==2-3*j: |
| 143 | +neighbors+= [(x-4*j,level+1)forxinrange(5)] |
| 144 | +elifposition==1-2*j: |
| 145 | +neighbors+= [(-y*j,level+1)foryinrange(5)] |
| 146 | +elifposition==3-2*j: |
| 147 | +neighbors+= [(4-y*j,level+1)foryinrange(5)] |
| 148 | + |
| 149 | +returnneighbors |
| 150 | + |
| 151 | +pathfinding.Graph.neighbors=neighbors |
| 152 | + |
| 153 | +empty_grid= ("."*5+"\n")*5 |
| 154 | +area=pathfinding.Graph() |
| 155 | +area.grid_to_vertices(puzzle_input) |
| 156 | +area.add_walls([2-2*j]) |
| 157 | + |
| 158 | +nb_minutes=200ifcase_to_test=="real"else10 |
| 159 | + |
| 160 | +recursive=pathfinding.Graph() |
| 161 | +recursive.vertices= { |
| 162 | + (position,level):"." |
| 163 | +forpositioninarea.vertices |
| 164 | +forlevelinrange(-nb_minutes//2,nb_minutes//2+1) |
| 165 | + } |
| 166 | + |
| 167 | +recursive.vertices.update( |
| 168 | + {(position,0):area.vertices[position]forpositioninarea.vertices} |
| 169 | + ) |
| 170 | + |
| 171 | +forgenerationinrange(nb_minutes): |
| 172 | +new_grids=pathfinding.Graph() |
| 173 | +new_grids.vertices= {} |
| 174 | +forpositioninrecursive.vertices: |
| 175 | +ifrecursive.vertices[position]=="#": |
| 176 | +living_neighbors=len( |
| 177 | + [ |
| 178 | +neighbor |
| 179 | +forneighborinrecursive.neighbors(position) |
| 180 | +ifrecursive.vertices.get(neighbor,".")=="#" |
| 181 | + ] |
| 182 | + ) |
| 183 | +ifliving_neighbors==1: |
| 184 | +new_grids.vertices[position]="#" |
| 185 | +else: |
| 186 | +new_grids.vertices[position]="." |
| 187 | +else: |
| 188 | +living_neighbors=len( |
| 189 | + [ |
| 190 | +neighbor |
| 191 | +forneighborinrecursive.neighbors(position) |
| 192 | +ifrecursive.vertices.get(neighbor,".")=="#" |
| 193 | + ] |
| 194 | + ) |
| 195 | +ifliving_neighborsin (1,2): |
| 196 | +new_grids.vertices[position]="#" |
| 197 | +else: |
| 198 | +new_grids.vertices[position]="." |
| 199 | + |
| 200 | +recursive.vertices=new_grids.vertices.copy() |
| 201 | + |
| 202 | +puzzle_actual_result=len( |
| 203 | + [xforxinrecursive.verticesifrecursive.vertices[x]=="#"] |
| 204 | + ) |
| 205 | + |
| 206 | + |
| 207 | +# -------------------------------- Outputs / results --------------------------------- # |
| 208 | + |
| 209 | +print("Expected result : "+str(puzzle_expected_result)) |
| 210 | +print("Actual result : "+str(puzzle_actual_result)) |