day17: super obvious code cleanups, more to do

This commit is contained in:
Keenan Tims 2023-12-17 15:18:22 -08:00
parent 9e37b2ce66
commit 3ee26cefe5
Signed by: ktims
GPG Key ID: 11230674D69038D4
4 changed files with 312 additions and 324 deletions

80
17/Cargo.lock generated
View File

@ -12,46 +12,24 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "day17"
version = "0.1.0"
dependencies = [
"petgraph",
"itertools",
"termcolor",
"test-case",
]
[[package]]
name = "equivalent"
version = "1.0.1"
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "fixedbitset"
version = "0.4.2"
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "indexmap"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "petgraph"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9"
dependencies = [
"fixedbitset",
"indexmap",
"either",
]
[[package]]
@ -83,6 +61,15 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449"
dependencies = [
"winapi-util",
]
[[package]]
name = "test-case"
version = "3.3.1"
@ -121,3 +108,34 @@ name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
petgraph = "0.6.4"
itertools = "0.12.0"
termcolor = "1.4.0"
test-case = "3.3.1"

View File

@ -1,6 +1,8 @@
use termcolor::Color;
pub struct ColorMap(&'static [Color]);
#[allow(dead_code)]
pub const COLORMAP_SMOOTH_COOLWARM: ColorMap = ColorMap(&[
Color::Rgb(59, 76, 19),
Color::Rgb(60, 78, 19),
@ -260,6 +262,7 @@ pub const COLORMAP_SMOOTH_COOLWARM: ColorMap = ColorMap(&[
Color::Rgb(180, 4, 38),
]);
#[allow(dead_code)]
pub const COLORMAP_INFERNO: ColorMap = ColorMap(&[
Color::Rgb(0, 0, 4),
Color::Rgb(1, 0, 5),

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();