day17: super obvious code cleanups, more to do

This commit is contained in:
2023-12-17 15:18:22 -08:00
parent 9e37b2ce66
commit 3ee26cefe5
4 changed files with 312 additions and 324 deletions

View File

@ -1,5 +1,4 @@
use colormap::ColorMap;
use itertools::{Itertools, MinMaxResult};
use std::collections::hash_map::RandomState;
use std::collections::{BinaryHeap, HashMap};
use std::fs::File;
@ -90,6 +89,7 @@ impl CityMap {
_ => None,
}
}
#[allow(dead_code)]
fn print(&self) -> Result<(), Box<dyn std::error::Error>> {
let cost_max = *self.map.iter().flat_map(|row| row.iter()).max().unwrap() as f64;
@ -115,7 +115,6 @@ type Position = (usize, usize);
struct WalkCost<'a> {
start: Position,
cost_from: Vec<Vec<HashMap<(Direction, usize), u64>>>,
dir_to: HashMap<Position, Direction>,
map: &'a CityMap,
}
@ -157,7 +156,6 @@ impl<'a> WalkCost<'a> {
.iter()
.map(|row| repeat(HashMap::new()).take(row.len()).collect())
.collect(),
dir_to: HashMap::new(),
}
}
fn compute(&mut self) {
@ -230,38 +228,6 @@ impl<'a> WalkCost<'a> {
}
}
}
fn shortest_path_to(&self, to: Position) -> Vec<(Position, Direction)> {
let mut path = Vec::new();
let mut cur_pos = to;
// start at the end, walk backwards
while cur_pos != self.start {
let dir = self.dir_to.get(&cur_pos).unwrap();
path.push((cur_pos, *dir));
cur_pos = self.map.offset_pos(cur_pos, &dir.opposite()).unwrap();
}
path
}
fn print_shortest_path(&self, to: Position) {
let path = self.shortest_path_to(to);
let map: HashMap<_, _, RandomState> = HashMap::from_iter(path.into_iter());
// for y in 0..self.cost_to.len() {
// for x in 0..self.map.map[y].len() {
// if let Some(to_dir) = map.get(&(x, y)) {
// let c = match to_dir {
// Direction::Left => '<',
// Direction::Right => '>',
// Direction::Up => '^',
// Direction::Down => 'v',
// };
// print!("{}", c);
// } else {
// print!("{}", self.map.map[y][x]);
// }
// }
// println!();
// }
}
}
struct WalkCost2<'a> {
@ -374,7 +340,7 @@ impl<'a> WalkCost2<'a> {
let mut cur_pos = to;
// start at the end, walk backwards
while cur_pos != self.start {
let (m, val) = self.cost_from[cur_pos.1][cur_pos.0]
let (m, _val) = self.cost_from[cur_pos.1][cur_pos.0]
.iter()
.min_by(|a, b| a.1.cmp(b.1))
.unwrap();