Minor optimizations for day12

This commit is contained in:
Timothy Warren 2022-12-22 09:08:14 -05:00
parent 374b363bb7
commit 9c2665ab61
1 changed files with 10 additions and 13 deletions

View File

@ -79,9 +79,9 @@ impl Node {
return self.parents.contains(&value); return self.parents.contains(&value);
} }
pub fn get_leaves(&self) -> Vec<Node> { pub fn get_leaves(&self) -> Vec<&Node> {
if self.is_leaf() { if self.is_leaf() {
return vec![self.to_owned()]; return vec![self];
} }
let mut leaves = Vec::new(); let mut leaves = Vec::new();
@ -110,7 +110,6 @@ impl Node {
#[derive(Debug)] #[derive(Debug)]
pub struct Pathfinder { pub struct Pathfinder {
current_elevation: char,
start_idx: usize, start_idx: usize,
end_idx: usize, end_idx: usize,
grid: Grid<char>, grid: Grid<char>,
@ -120,7 +119,6 @@ pub struct Pathfinder {
impl Pathfinder { impl Pathfinder {
pub fn from_file_str(file_str: &str) -> Self { pub fn from_file_str(file_str: &str) -> Self {
let mut pf = Pathfinder { let mut pf = Pathfinder {
current_elevation: 'a',
start_idx: 0, start_idx: 0,
end_idx: 0, end_idx: 0,
grid: Grid::from_file_str(file_str), grid: Grid::from_file_str(file_str),
@ -137,7 +135,6 @@ impl Pathfinder {
self.start_idx = start; self.start_idx = start;
self.end_idx = end; self.end_idx = end;
self.current_elevation = 'a';
self.grid.vec[start] = 'a'; self.grid.vec[start] = 'a';
self.grid.vec[end] = 'z'; self.grid.vec[end] = 'z';
self.tree.idx = start; self.tree.idx = start;
@ -254,22 +251,19 @@ impl Pathfinder {
self.tree = tree; self.tree = tree;
} }
fn get_paths(&self) -> Vec<Node> { fn get_paths(&self) -> impl Iterator<Item= &Node> {
self.tree self.tree
.get_leaves() .get_leaves()
.into_iter() .into_iter()
.filter(|n| n.idx == self.end_idx) .filter(|n| n.idx == self.end_idx)
.collect()
} }
pub fn find_shortest_path(&mut self) -> Node { pub fn find_shortest_path(&mut self) -> &Node {
self.build_tree(); self.build_tree();
self.get_paths() self.get_paths()
.iter()
.min_by(|a, b| a.get_len().cmp(&b.get_len())) .min_by(|a, b| a.get_len().cmp(&b.get_len()))
.unwrap() .unwrap()
.to_owned()
} }
} }
@ -278,9 +272,11 @@ impl Pathfinder {
fn main() { fn main() {
let file_str = include_str!("input.txt"); let file_str = include_str!("input.txt");
let mut finder = Pathfinder::from_file_str(file_str); let mut finder = Pathfinder::from_file_str(file_str);
let shortest_path = finder.find_shortest_path();
println!("Part 1: Fewest steps: {}", shortest_path.get_len()); dbg!(finder);
// let shortest_path = finder.find_shortest_path();
// println!("Part 1: Fewest steps: {}", shortest_path.get_len());
} }
#[cfg(test)] #[cfg(test)]
@ -305,7 +301,8 @@ mod tests {
#[test] #[test]
fn find_shortest_path() { fn find_shortest_path() {
let shortest = get_finder().find_shortest_path(); let mut finder = get_finder();
let shortest = finder.find_shortest_path();
assert_eq!(shortest.get_len(), 31); assert_eq!(shortest.get_len(), 31);
} }