|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | + |
| 6 | +test_data = {} |
| 7 | + |
| 8 | +test = 1 |
| 9 | +test_data[test] = { |
| 10 | + "input": """ A |
| 11 | + A |
| 12 | + #################.############# |
| 13 | + #.#...#...................#.#.# |
| 14 | + #.#.#.###.###.###.#########.#.# |
| 15 | + #.#.#.......#...#.....#.#.#...# |
| 16 | + #.#########.###.#####.#.#.###.# |
| 17 | + #.............#.#.....#.......# |
| 18 | + ###.###########.###.#####.#.#.# |
| 19 | + #.....# A C #.#.#.# |
| 20 | + ####### S P #####.# |
| 21 | + #.#...# #......VT |
| 22 | + #.#.#.# #.##### |
| 23 | + #...#.# YN....#.# |
| 24 | + #.###.# #####.# |
| 25 | +DI....#.# #.....# |
| 26 | + #####.# #.###.# |
| 27 | +ZZ......# QG....#..AS |
| 28 | + ###.### ####### |
| 29 | +JO..#.#.# #.....# |
| 30 | + #.#.#.# ###.#.# |
| 31 | + #...#..DI BU....#..LF |
| 32 | + #####.# #.##### |
| 33 | +YN......# VT..#....QG |
| 34 | + #.###.# #.###.# |
| 35 | + #.#...# #.....# |
| 36 | + ###.### J L J #.#.### |
| 37 | + #.....# O F P #.#...# |
| 38 | + #.###.#####.#.#####.#####.###.# |
| 39 | + #...#.#.#...#.....#.....#.#...# |
| 40 | + #.#####.###.###.#.#.#########.# |
| 41 | + #...#.#.....#...#.#.#.#.....#.# |
| 42 | + #.###.#####.###.###.#.#.####### |
| 43 | + #.#.........#...#.............# |
| 44 | + #########.###.###.############# |
| 45 | + B J C |
| 46 | + U P P """, |
| 47 | + "expected": ["58", "Unknown"], |
| 48 | +} |
| 49 | + |
| 50 | +test += 1 |
| 51 | +test_data[test] = { |
| 52 | + "input": """ Z L X W C |
| 53 | + Z P Q B K |
| 54 | + ###########.#.#.#.#######.############### |
| 55 | + #...#.......#.#.......#.#.......#.#.#...# |
| 56 | + ###.#.#.#.#.#.#.#.###.#.#.#######.#.#.### |
| 57 | + #.#...#.#.#...#.#.#...#...#...#.#.......# |
| 58 | + #.###.#######.###.###.#.###.###.#.####### |
| 59 | + #...#.......#.#...#...#.............#...# |
| 60 | + #.#########.#######.#.#######.#######.### |
| 61 | + #...#.# F R I Z #.#.#.# |
| 62 | + #.###.# D E C H #.#.#.# |
| 63 | + #.#...# #...#.# |
| 64 | + #.###.# #.###.# |
| 65 | + #.#....OA WB..#.#..ZH |
| 66 | + #.###.# #.#.#.# |
| 67 | +CJ......# #.....# |
| 68 | + ####### ####### |
| 69 | + #.#....CK #......IC |
| 70 | + #.###.# #.###.# |
| 71 | + #.....# #...#.# |
| 72 | + ###.### #.#.#.# |
| 73 | +XF....#.# RF..#.#.# |
| 74 | + #####.# ####### |
| 75 | + #......CJ NM..#...# |
| 76 | + ###.#.# #.###.# |
| 77 | +RE....#.# #......RF |
| 78 | + ###.### X X L #.#.#.# |
| 79 | + #.....# F Q P #.#.#.# |
| 80 | + ###.###########.###.#######.#########.### |
| 81 | + #.....#...#.....#.......#...#.....#.#...# |
| 82 | + #####.#.###.#######.#######.###.###.#.#.# |
| 83 | + #.......#.......#.#.#.#.#...#...#...#.#.# |
| 84 | + #####.###.#####.#.#.#.#.###.###.#.###.### |
| 85 | + #.......#.....#.#...#...............#...# |
| 86 | + #############.#.#.###.################### |
| 87 | + A O F N |
| 88 | + A A D M """, |
| 89 | + "expected": ["Unknown", "396"], |
| 90 | +} |
| 91 | + |
| 92 | +test = "real" |
| 93 | +input_file = os.path.join( |
| 94 | + os.path.dirname(__file__), |
| 95 | + "Inputs", |
| 96 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 97 | +) |
| 98 | +test_data[test] = { |
| 99 | + "input": open(input_file, "r+").read(), |
| 100 | + "expected": ["642", "7492"], |
| 101 | +} |
| 102 | + |
| 103 | +# -------------------------------- Control program execution ------------------------- # |
| 104 | + |
| 105 | +case_to_test = 2 |
| 106 | +part_to_test = 2 |
| 107 | + |
| 108 | +# -------------------------------- Initialize some variables ------------------------- # |
| 109 | + |
| 110 | +puzzle_input = test_data[case_to_test]["input"] |
| 111 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 112 | +puzzle_actual_result = "Unknown" |
| 113 | + |
| 114 | + |
| 115 | +# -------------------------------- Actual code execution ----------------------------- # |
| 116 | +def grid_to_vertices(self, grid, diagonals_allowed=False, wall="#"): |
| 117 | + self.vertices = {} |
| 118 | + y = 0 |
| 119 | + |
| 120 | + for line in grid.splitlines(): |
| 121 | + for x in range(len(line)): |
| 122 | + if line[x] != wall: |
| 123 | + self.vertices[x - y * j] = line[x] |
| 124 | + y += 1 |
| 125 | + |
| 126 | + for source in self.vertices: |
| 127 | + for direction in directions_straight: |
| 128 | + target = source + direction |
| 129 | + if target in self.vertices: |
| 130 | + if source in self.edges: |
| 131 | + self.edges[source][target] = 1 |
| 132 | + else: |
| 133 | + self.edges[source] = {target: 1} |
| 134 | + |
| 135 | + return True |
| 136 | + |
| 137 | + |
| 138 | +pathfinding.WeightedGraph.grid_to_vertices = grid_to_vertices |
| 139 | + |
| 140 | + |
| 141 | +grid = pathfinding.WeightedGraph() |
| 142 | +grid.grid_to_vertices(puzzle_input.replace(" ", "#")) |
| 143 | +width, height = max_real(grid.vertices), -min_imag(grid.vertices) |
| 144 | +letters = grid.grid_search(puzzle_input, "abcdefghijklmnopqrstuvwxyz".upper()) |
| 145 | +portals = {} |
| 146 | +for letter in letters: |
| 147 | + for position in letters[letter]: |
| 148 | + # Vertical portal |
| 149 | + if ( |
| 150 | + grid.vertices.get(position + south, "#") |
| 151 | + in "abcdefghijklmnopqrstuvwxyz".upper() |
| 152 | + ): |
| 153 | + portal = letter + grid.vertices[position + south] |
| 154 | + if grid.vertices.get(position + south * 2, "#") == ".": |
| 155 | + portal_position = position + south * 2 |
| 156 | + else: |
| 157 | + portal_position = position - south |
| 158 | + |
| 159 | + # Horizontal portal |
| 160 | + elif ( |
| 161 | + grid.vertices.get(position + east, "#") |
| 162 | + in "abcdefghijklmnopqrstuvwxyz".upper() |
| 163 | + ): |
| 164 | + portal = letter + grid.vertices[position + east] |
| 165 | + if grid.vertices.get(position + east * 2, "#") == ".": |
| 166 | + portal_position = position + east * 2 |
| 167 | + else: |
| 168 | + portal_position = position - east |
| 169 | + else: |
| 170 | + continue |
| 171 | + |
| 172 | + portal_position = SuperComplex(portal_position) |
| 173 | + |
| 174 | + # Find whether we're at the center or not (I don't care for AA or ZZ) |
| 175 | + if portal in ("AA", "ZZ"): |
| 176 | + portals[portal] = portal_position |
| 177 | + elif portal_position.real == 2 or portal_position.real == width - 2: |
| 178 | + portals[(portal, "out")] = portal_position |
| 179 | + elif portal_position.imag == -2 or portal_position.imag == -(height - 2): |
| 180 | + portals[(portal, "out")] = portal_position |
| 181 | + else: |
| 182 | + portals[(portal, "in")] = portal_position |
| 183 | + |
| 184 | + |
| 185 | +if part_to_test == 1: |
| 186 | + for portal in portals: |
| 187 | + if len(portal) == 2 and portal[1] == "in": |
| 188 | + portal_in = portals[portal] |
| 189 | + portal_out = portals[(portal[0], "out")] |
| 190 | + grid.edges[portal_in][portal_out] = 1 |
| 191 | + grid.edges[portal_in][portal_out] = 1 |
| 192 | + |
| 193 | + grid.dijkstra(portals["AA"], portals["ZZ"]) |
| 194 | + puzzle_actual_result = grid.distance_from_start[portals["ZZ"]] |
| 195 | + |
| 196 | + |
| 197 | +else: |
| 198 | + edges = {} |
| 199 | + for portal in portals: |
| 200 | + grid.reset_search() |
| 201 | + grid.dijkstra(portals[portal]) |
| 202 | + for other_portal in portals: |
| 203 | + if portal == other_portal: |
| 204 | + continue |
| 205 | + if not portals[other_portal] in grid.distance_from_start: |
| 206 | + continue |
| 207 | + distance = grid.distance_from_start[portals[other_portal]] |
| 208 | + for level in range(20): |
| 209 | + if portal in ("AA", "ZZ") and level != 0: |
| 210 | + break |
| 211 | + if other_portal in ("AA", "ZZ") and level != 0: |
| 212 | + break |
| 213 | + if (portal, level) in edges: |
| 214 | + edges[(portal, level)].update({(other_portal, level): distance}) |
| 215 | + else: |
| 216 | + edges[(portal, level)] = {(other_portal, level): distance} |
| 217 | + |
| 218 | + if len(portal) == 2 and portal[1] == "in": |
| 219 | + portal_out = (portal[0], "out") |
| 220 | + edges[(portal, level)].update({(portal_out, level + 1): 1}) |
| 221 | + elif len(portal) == 2 and portal[1] == "out" and level != 0: |
| 222 | + portal_in = (portal[0], "in") |
| 223 | + edges[(portal, level)].update({(portal_in, level - 1): 1}) |
| 224 | + |
| 225 | + grid = pathfinding.WeightedGraph({}, edges) |
| 226 | + |
| 227 | + grid.dijkstra(("AA", 0), ("ZZ", 0)) |
| 228 | + puzzle_actual_result = grid.distance_from_start[("ZZ", 0)] |
| 229 | + |
| 230 | +# -------------------------------- Outputs / results --------------------------------- # |
| 231 | + |
| 232 | +print("Expected result : " + str(puzzle_expected_result)) |
| 233 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments