From 3046c118d13757219a274b904368df41156474e2 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Tue, 13 Dec 2022 10:18:54 +0100 Subject: Improved day12 --- day12/__init__.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'day12') 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): self, map: Map, start: Tuple[int, int], - end: Tuple[int, int], + end: Callable[[int, int], bool], distance: Callable[[Tuple[int, int], Tuple[int, int]], int], heuristic: Callable[[Tuple[int, int]], int], ) -> List[Tuple[int, int]]: @@ -94,7 +94,7 @@ class Assignment(BaseAssignment, ABC): except IndexError: raise RuntimeError("No path found") - if current == end: + if end(*current): path = [current] while current in came_from: @@ -126,7 +126,7 @@ class AssignmentOne(Assignment): path = self.a_star( map, map.start, - map.end, + lambda x, y: (x, y) == map.end, distance=lambda a, b: self.distance(map, a, b), heuristic=lambda a: self.heuristic(map, a), ) @@ -139,20 +139,13 @@ class AssignmentTwo(Assignment): def run(self, input: Iterator) -> Any: map = Map(list(input)) - lengths = [] - for a in map.find_position("a"): - try: - path = self.a_star( - map, - a, - map.end, - distance=lambda a, b: self.distance(map, a, b), - heuristic=lambda a: self.heuristic(map, a), - ) - - lengths.append(len(path) - 1) - except RuntimeError: - continue - - return sorted(lengths)[0] + path = self.a_star( + map, + map.end, + lambda x, y: map.elevation(x, y) == 1, + distance=lambda a, b: self.distance(map, b, a), + heuristic=lambda a: 1, + ) + + return len(path) - 1 -- cgit v1.2.3