Finish day 12
parent
e7f95fc243
commit
81bf01201a
58
src/main.rs
58
src/main.rs
|
@ -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()
|
||||||
|
.map(move |(y, level)| (x, y, *level))
|
||||||
|
.filter_map(|(x, y, level)| if level == 0 { Some((x, y)) } else { None })
|
||||||
|
})
|
||||||
|
.flat_map(|start| self.navigate(start))
|
||||||
|
.min()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
let level = self.map[x][y];
|
||||||
print!("moving from ({x},{y}) to ");
|
if (x, y) == self.end {
|
||||||
prior = last;
|
return Some(steps);
|
||||||
last = (x, y);
|
}
|
||||||
(x, y, _) = self
|
for (nx, ny, nlevel) in self.neighbors(x, y) {
|
||||||
.neighbors(x, y)
|
if !visited.contains(&(nx, ny)) && nlevel <= level + 1 {
|
||||||
.filter(|(_, _, neighbor)| (level..=level + 1).contains(neighbor))
|
visited.insert((nx, ny));
|
||||||
.filter(|(nx, ny, _)| (*nx, *ny) != prior)
|
q.push_back(((nx, ny), steps + 1))
|
||||||
.max_by_key(|(_, _, neighbor)| *neighbor)
|
}
|
||||||
.unwrap();
|
}
|
||||||
println!("({x},{y})");
|
|
||||||
steps += 1;
|
|
||||||
}
|
}
|
||||||
steps
|
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)> {
|
||||||
|
|
Loading…
Reference in New Issue