2022 rust day 12
This commit is contained in:
parent
78af1f8bac
commit
36d8191f67
|
@ -1,3 +1,4 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::collections::{BinaryHeap, HashMap};
|
use std::collections::{BinaryHeap, HashMap};
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
@ -34,6 +35,7 @@ impl PartialOrd for State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn neighbours((row, col): (usize, usize), (rows, cols): (usize, usize)) -> Vec<(usize, usize)> {
|
fn neighbours((row, col): (usize, usize), (rows, cols): (usize, usize)) -> Vec<(usize, usize)> {
|
||||||
let mut ns = Vec::with_capacity(4);
|
let mut ns = Vec::with_capacity(4);
|
||||||
if row + 1 < rows { ns.push((row + 1, col)) };
|
if row + 1 < rows { ns.push((row + 1, col)) };
|
||||||
|
@ -74,9 +76,10 @@ fn a_star(start: (usize, usize), goal: (usize, usize), grid: &Matrix<u8>) -> Opt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unwind path
|
||||||
let mut path = vec![goal];
|
let mut path = vec![goal];
|
||||||
while !path.last()?.eq(&start) {
|
while !path.last().unwrap().eq(&start) {
|
||||||
path.push(*parent.get(path.last()?)?);
|
path.push(*parent.get(path.last().unwrap())?);
|
||||||
}
|
}
|
||||||
Some(path)
|
Some(path)
|
||||||
}
|
}
|
||||||
|
@ -85,18 +88,13 @@ fn solve1((start, goal, grid): &Data) -> usize {
|
||||||
a_star(*start, *goal, grid).unwrap().len() - 1
|
a_star(*start, *goal, grid).unwrap().len() - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_low_points(grid: &Matrix<u8>) -> Vec<(usize, usize)> {
|
|
||||||
|
fn solve2((_, goal, grid): &Data) -> usize {
|
||||||
grid.cell_indices()
|
grid.cell_indices()
|
||||||
.filter_map(|index| match grid[index] == 0 {
|
.filter_map(|index| match grid[index] == 0 {
|
||||||
true => Some(index),
|
true => Some(index),
|
||||||
false => None
|
false => None
|
||||||
})
|
})
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn solve2((_, goal, grid): &Data) -> usize {
|
|
||||||
find_low_points(grid)
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|start|
|
.filter_map(|start|
|
||||||
a_star(start, *goal, grid).map(|path| path.len() - 1)
|
a_star(start, *goal, grid).map(|path| path.len() - 1)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue