From 9a4ac427e00edd84ec7ccecb3dec53cf3da98bd8 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 15 Dec 2021 18:29:08 +0100 Subject: [PATCH] Use Dijkstra instead of A* The available distance heuristic doesn't save enough steps to offset its overhead. --- 2021/src/day15.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/2021/src/day15.rs b/2021/src/day15.rs index 3565b33..0318ad5 100644 --- a/2021/src/day15.rs +++ b/2021/src/day15.rs @@ -69,13 +69,13 @@ impl Map { pub fn shortest_path(&self, start: Point, end: Point) -> u32 { let mut todo = BinaryHeap::new(); - todo.push(Reverse((Self::manhattan(start, end), 0, start))); + todo.push(Reverse((0, start))); let mut visited = vec![false; self.data.len()]; let height = self.height() as i32; - while let Some(Reverse((_, distance, pos))) = todo.pop() { + while let Some(Reverse((distance, pos))) = todo.pop() { if pos == end { return distance; } @@ -106,9 +106,8 @@ impl Map { } let new_distance = distance + self.data[index] as u32; - let new_guess = Self::manhattan(new, end) + new_distance; - todo.push(Reverse((new_guess, new_distance, new))); + todo.push(Reverse((new_distance, new))); } } } @@ -116,10 +115,6 @@ impl Map { panic!("No route found from {:?} to {:?}", start, end); } - fn manhattan((xa, ya): Point, (xb, yb): Point) -> u32 { - (xa - xb).abs() as u32 + (ya - yb).abs() as u32 - } - pub fn height(&self) -> usize { self.data.len() / self.width }