summaryrefslogtreecommitdiffstats
path: root/day12/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'day12/__init__.py')
-rw-r--r--day12/__init__.py31
1 files changed, 12 insertions, 19 deletions
diff --git a/day12/__init__.py b/day12/__init__.py
index 55545f7..69f9458 100644
--- a/day12/__init__.py
+++ b/day12/__init__.py
@@ -75,7 +75,7 @@ class Assignment(BaseAssignment, ABC):
75 self, 75 self,
76 map: Map, 76 map: Map,
77 start: Tuple[int, int], 77 start: Tuple[int, int],
78 end: Tuple[int, int], 78 end: Callable[[int, int], bool],
79 distance: Callable[[Tuple[int, int], Tuple[int, int]], int], 79 distance: Callable[[Tuple[int, int], Tuple[int, int]], int],
80 heuristic: Callable[[Tuple[int, int]], int], 80 heuristic: Callable[[Tuple[int, int]], int],
81 ) -> List[Tuple[int, int]]: 81 ) -> List[Tuple[int, int]]:
@@ -94,7 +94,7 @@ class Assignment(BaseAssignment, ABC):
94 except IndexError: 94 except IndexError:
95 raise RuntimeError("No path found") 95 raise RuntimeError("No path found")
96 96
97 if current == end: 97 if end(*current):
98 path = [current] 98 path = [current]
99 99
100 while current in came_from: 100 while current in came_from:
@@ -126,7 +126,7 @@ class AssignmentOne(Assignment):
126 path = self.a_star( 126 path = self.a_star(
127 map, 127 map,
128 map.start, 128 map.start,
129 map.end, 129 lambda x, y: (x, y) == map.end,
130 distance=lambda a, b: self.distance(map, a, b), 130 distance=lambda a, b: self.distance(map, a, b),
131 heuristic=lambda a: self.heuristic(map, a), 131 heuristic=lambda a: self.heuristic(map, a),
132 ) 132 )
@@ -139,20 +139,13 @@ class AssignmentTwo(Assignment):
139 139
140 def run(self, input: Iterator) -> Any: 140 def run(self, input: Iterator) -> Any:
141 map = Map(list(input)) 141 map = Map(list(input))
142 lengths = []
143 142
144 for a in map.find_position("a"): 143 path = self.a_star(
145 try: 144 map,
146 path = self.a_star( 145 map.end,
147 map, 146 lambda x, y: map.elevation(x, y) == 1,
148 a, 147 distance=lambda a, b: self.distance(map, b, a),
149 map.end, 148 heuristic=lambda a: 1,
150 distance=lambda a, b: self.distance(map, a, b), 149 )
151 heuristic=lambda a: self.heuristic(map, a), 150
152 ) 151 return len(path) - 1
153
154 lengths.append(len(path) - 1)
155 except RuntimeError:
156 continue
157
158 return sorted(lengths)[0]