From 092aba848aa701d943896c8370b5bdb85a2d5643 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 25 Dec 2022 00:09:40 +0100 Subject: [PATCH] Added hueristic to speed up the pathfinding --- 2022/src/bin/day24.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/2022/src/bin/day24.rs b/2022/src/bin/day24.rs index 32e2c70..820bba2 100644 --- a/2022/src/bin/day24.rs +++ b/2022/src/bin/day24.rs @@ -101,6 +101,16 @@ impl Map { ]; while queue.len() > 0 { + // Pretty sure this is basically A*? + // Not 100% sure if it actually works correctly, but it works correctly for my input ¯\_(ツ)_/¯ + queue.make_contiguous().sort_by(|a, b| { + let distance_a = (a.position.x - end.x).abs() + (a.position.y - end.y).abs() + a.time_passed as isize; + let distance_b = (b.position.x - end.x).abs() + (b.position.y - end.y).abs() + b.time_passed as isize; + + distance_a.cmp(&distance_b) + }); + + // Instead we should pick the entry that is the closest to our target first let current = queue.pop_front().unwrap(); // Check every neighbour