Finished day 12 aoc part 1 with pathfinding library and it work woopwoop

This commit is contained in:
2023-01-09 03:19:55 +01:00
parent 45b01ca65c
commit a958e1954b
3 changed files with 148 additions and 10 deletions

View File

@@ -1,13 +1,16 @@
use std::collections::HashMap;
use pathfinding::prelude::dijkstra;
fn main() {
let test_input = include_str!("../test.txt")
let test_input = include_str!("../input.txt")
.split_whitespace()
.collect::<Vec<&str>>();
let mut start = (0, 0);
let mut end = (0, 0);
let mut points : HashMap<(u32, u32), u32> = HashMap::new();
// let mut max_y = 0;
// let mut max_x = 0;
let mut points : HashMap<(i32, i32), i32> = HashMap::new();
let mut y = 0;
for line in test_input {
@@ -23,9 +26,45 @@ fn main() {
end = (x, y);
}
points.insert((x, y), c as u32 - 'a' as u32);
points.insert((x, y), c as i32 - 'a' as i32);
// max_x = x;
x += 1;
}
// max_y = y;
y += 1;
}
let r = dijkstra(
&start,
|&(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);
println!("Number of steps taken: {}", r.expect("No path found").1);
// Testing the parsing
// for y in 0..=max_y {
// for x in 0..=max_x {
// if (x, y) == start {
// print!("S");
// } else if (x, y) == end {
// print!("E")
// } else {
// let c = from_u32((points.get(&(x, y)).unwrap() + 'a' as i32) as u32).unwrap();
// print!("{c}");
// }
// }
// println!();
// }
}