Finished day 12 aoc with nice iterators
This commit is contained in:
@@ -7,6 +7,7 @@ fn main() {
|
|||||||
.collect::<Vec<&str>>();
|
.collect::<Vec<&str>>();
|
||||||
|
|
||||||
let mut start = (0, 0);
|
let mut start = (0, 0);
|
||||||
|
let mut starts = Vec::new();
|
||||||
let mut end = (0, 0);
|
let mut end = (0, 0);
|
||||||
// let mut max_y = 0;
|
// let mut max_y = 0;
|
||||||
// let mut max_x = 0;
|
// let mut max_x = 0;
|
||||||
@@ -25,6 +26,9 @@ fn main() {
|
|||||||
c = 'z';
|
c = 'z';
|
||||||
end = (x, y);
|
end = (x, y);
|
||||||
}
|
}
|
||||||
|
if c == 'a' {
|
||||||
|
starts.push((x, y));
|
||||||
|
}
|
||||||
|
|
||||||
points.insert((x, y), c as i32 - 'a' as i32);
|
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);
|
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
|
// Testing the parsing
|
||||||
|
|
||||||
// for y in 0..=max_y {
|
// for y in 0..=max_y {
|
||||||
|
|||||||
Reference in New Issue
Block a user