Skip to content

Commit

Permalink
AoC 2023 Day 23 - faster
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 23, 2023
1 parent beabd9d commit 08e81ab
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/python/AoC2023_23.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,24 @@ def __init__(self, grid: CharGrid) -> None:
def find_longest_hike_length_with_only_downward_slopes(self) -> int:
forks = self._find_forks()
graph = self._build_graph(forks, downward_slope_only=True)
return self._find_longest(graph, self.start, set[Cell]())
return self._find_longest(graph, self.start, self.end, set[Cell]())

def find_longest_hike_length(self) -> int:
forks = self._find_forks()
graph = self._build_graph(forks, downward_slope_only=False)
return self._find_longest(graph, self.start, set[Cell]())
dist_from_start: int = next(_ for _ in graph[self.start])[1]
dist_to_end: int = next(_ for _ in graph[self.end])[1]
new_start = next(
k for k, v in graph.items() if (self.start, dist_from_start) in v
)
new_end = next(
k for k, v in graph.items() if (self.end, dist_to_end) in v
)
return (
dist_from_start
+ dist_to_end
+ self._find_longest(graph, new_start, new_end, set[Cell]())
)

def _find_forks(self) -> set[Cell]:
def is_fork(cell: Cell) -> bool:
Expand Down Expand Up @@ -108,16 +120,22 @@ def _build_graph(
return graph

def _find_longest(
self, graph: dict[Cell, set[Edge]], curr: Cell, seen: set[Cell]
self,
graph: dict[Cell, set[Edge]],
curr: Cell,
end: Cell,
seen: set[Cell],
) -> int:
if curr == self.end:
if curr == end:
return 0
ans = -1_000_000_000
seen.add(curr)
for vertex, distance in graph[curr]:
if vertex in seen:
continue
ans = max(ans, distance + self._find_longest(graph, vertex, seen))
ans = max(
ans, distance + self._find_longest(graph, vertex, end, seen)
)
seen.remove(curr)
return ans

Expand Down

0 comments on commit 08e81ab

Please sign in to comment.