From a74f02638bc69f5fe16b90d7b63e83243d7ff0c5 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Mon, 9 Jan 2023 03:34:17 +0100 Subject: [PATCH] Finished day 12 aoc with nice iterators --- advent_of_code/2022/12/src/main.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/advent_of_code/2022/12/src/main.rs b/advent_of_code/2022/12/src/main.rs index f5ca6cc..47b10ff 100644 --- a/advent_of_code/2022/12/src/main.rs +++ b/advent_of_code/2022/12/src/main.rs @@ -7,6 +7,7 @@ fn main() { .collect::>(); let mut start = (0, 0); + let mut starts = Vec::new(); let mut end = (0, 0); // let mut max_y = 0; // let mut max_x = 0; @@ -25,6 +26,9 @@ fn main() { c = 'z'; end = (x, y); } + if c == 'a' { + starts.push((x, y)); + } points.insert((x, y), c as i32 - 'a' as i32); @@ -52,6 +56,25 @@ fn main() { println!("Number of steps taken: {}", r.expect("No path found").1); + let r = starts.into_iter().map(|s| dijkstra( + &s, + |&(x, y)| vec![((x-1, y), points.get(&(x-1, y)), (x, y)), + ((x+1, y), points.get(&(x+1, y)), (x, y)), + ((x, y+1), points.get(&(x, y+1)), (x, y)), + ((x, y-1), points.get(&(x, y-1)), (x, y)),] + .into_iter() + .filter(|&(_, n, c)| match n { + None => false, + Some(&height) => height <= *points.get(&c).unwrap() + 1, + }) + .map(|(coords, _, _)| (coords, 1)), + |&n| n == end)) + .filter_map(|r| r) + .map(|(_, r)| r) + .min(); + + println!("Number of steps taken from any a: {}", r.expect("No paths found")); + // Testing the parsing // for y in 0..=max_y {