Finish day 12

main
mat ess 2022-12-12 22:10:32 -05:00
parent e7f95fc243
commit 81bf01201a
1 changed files with 36 additions and 22 deletions

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::ops::{Range, RangeInclusive}; use std::ops::{Range, RangeInclusive};
@ -279,11 +279,12 @@ solutions! {
// day 12 part 1 // day 12 part 1
|input| { |input| {
let map = Map::from_string(input); let map = Map::from_string(input);
map.navigate().into() map.navigate(map.start).unwrap().into()
}, },
// day 12 part 1 // day 12 part 1
|input| { |input| {
todo!() let map = Map::from_string(input);
map.best_navigate().into()
}, },
] ]
} }
@ -296,27 +297,40 @@ struct Map {
} }
impl Map { impl Map {
fn navigate(self) -> u64 { fn best_navigate(self) -> u64 {
// this is fuckin buggy self.map
let (mut x, mut y) = self.start; .iter()
let mut last = (x, y); .enumerate()
let mut prior = (x, y); .flat_map(|(x, row)| {
let mut steps = 0; row.iter()
while (x, y) != self.end { .enumerate()
let level = self.map[x][y]; .map(move |(y, level)| (x, y, *level))
print!("moving from ({x},{y}) to "); .filter_map(|(x, y, level)| if level == 0 { Some((x, y)) } else { None })
prior = last; })
last = (x, y); .flat_map(|start| self.navigate(start))
(x, y, _) = self .min()
.neighbors(x, y) .unwrap()
.filter(|(_, _, neighbor)| (level..=level + 1).contains(neighbor))
.filter(|(nx, ny, _)| (*nx, *ny) != prior)
.max_by_key(|(_, _, neighbor)| *neighbor)
.unwrap();
println!("({x},{y})");
steps += 1;
} }
steps
fn navigate(&self, start: (usize, usize)) -> Option<u64> {
let mut q = VecDeque::new();
let mut visited = HashSet::new();
visited.insert(start);
q.push_back((start, 0));
while !q.is_empty() {
let ((x, y), steps) = q.pop_front().unwrap();
let level = self.map[x][y];
if (x, y) == self.end {
return Some(steps);
}
for (nx, ny, nlevel) in self.neighbors(x, y) {
if !visited.contains(&(nx, ny)) && nlevel <= level + 1 {
visited.insert((nx, ny));
q.push_back(((nx, ny), steps + 1))
}
}
}
None
} }
fn neighbors(&self, x: usize, y: usize) -> impl Iterator<Item = (usize, usize, isize)> { fn neighbors(&self, x: usize, y: usize) -> impl Iterator<Item = (usize, usize, isize)> {