From a81545a0129010e86574ae2e1f8d7a0672b2d7ae Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2022 23:53:07 +0000 Subject: [PATCH] Day 12, Part 2! --- 2022/day12/src/main.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/2022/day12/src/main.rs b/2022/day12/src/main.rs index 10d1228..06080b1 100644 --- a/2022/day12/src/main.rs +++ b/2022/day12/src/main.rs @@ -57,7 +57,7 @@ impl PartialOrd for State { /// An implemetation of Dijkstra's algorithm to find the shortest path from start to end and return /// its length. -fn find_length_of_shortest_path(squares: &Vec, start: usize, end: usize) -> u32 { +fn find_length_of_shortest_path(squares: &Vec, start: usize, end: usize) -> Option { let mut distances: Vec<_> = (0..squares.len()).map(|_| u32::MAX).collect(); let mut heap = BinaryHeap::new(); @@ -67,7 +67,7 @@ fn find_length_of_shortest_path(squares: &Vec, start: usize, end: usize) while let Some(State { node, cost }) = heap.pop() { if node == end { - return cost; + return Some(cost); } if cost > distances[node] { @@ -91,7 +91,7 @@ fn find_length_of_shortest_path(squares: &Vec, start: usize, end: usize) } } - 0 + None } fn main() { @@ -159,8 +159,18 @@ fn main() { } } - dbg!(&squares); - - let length_of_shortest_path = find_length_of_shortest_path(&squares, start, end); + let length_of_shortest_path = find_length_of_shortest_path(&squares, start, end).unwrap(); println!("Part 1: length of shortest path to location with best signal: {length_of_shortest_path}"); + + let mut paths_from_all_a_squares: Vec<(usize, u32)> = squares + .iter() + .enumerate() + .filter(|(_, sq)| sq.elevation() == 0) + .map(|(i, _)| (i, find_length_of_shortest_path(&squares, i, end))) + .filter(|(_, distance)| distance.is_some()) + .map(|(i, distance)| (i, distance.unwrap())) + .collect(); + paths_from_all_a_squares.sort_by(|a, b| b.1.cmp(&a.1)); + + dbg!(&paths_from_all_a_squares.last()); }