diff --git a/src/day16.rs b/src/day16.rs index 72843c3..9de464d 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -1,11 +1,14 @@ use aoc_runner_derive::aoc; -use grid::{AsCoord2d, Coord2d, Grid}; +use grid::Grid; use std::{ collections::{BinaryHeap, HashMap}, str::FromStr, usize, }; +type CoordType = i16; +type Coord = (CoordType, CoordType); + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)] enum FacingDirection { East, @@ -15,7 +18,7 @@ enum FacingDirection { } impl FacingDirection { - fn ofs(&self) -> (i32, i32) { + fn ofs(&self) -> (CoordType, CoordType) { match self { FacingDirection::East => (1, 0), FacingDirection::South => (0, 1), @@ -35,7 +38,7 @@ impl FacingDirection { #[derive(Clone, Eq, PartialEq, Debug)] struct State { cost: usize, - position: (i32, i32), + position: Coord, facing: FacingDirection, } @@ -58,7 +61,7 @@ impl PartialOrd for State { #[derive(Clone, Eq, PartialEq, Debug)] struct PathState { state: State, - path: Vec<(i32, i32)>, + path: Vec, } impl Ord for PathState { @@ -88,10 +91,10 @@ impl FromStr for Maze { impl Maze { fn dijkstra(&self) -> usize { let (start_x, start_y) = self.map.find(&b'S').expect("can't find start"); - let start = (start_x as i32, start_y as i32); + let start = (start_x as CoordType, start_y as CoordType); let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish"); - let finish = (finish_x as i32, finish_y as i32); + let finish = (finish_x as CoordType, finish_y as CoordType); let mut distances = HashMap::new(); let mut queue = BinaryHeap::new(); @@ -134,7 +137,7 @@ impl Maze { } usize::MAX } - fn path_dijkstra(&mut self) -> (usize, Vec>) { + fn path_dijkstra(&mut self) -> (usize, Vec>) { let (start_x, start_y) = self.map.find(&b'S').expect("can't find start"); let start = (start_x.try_into().unwrap(), start_y.try_into().unwrap()); let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish"); diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index 596268e..35b48ee 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -63,60 +63,22 @@ impl AsCoord2d for &Coord2d { } } -impl AsCoord2d for (i32, i32) { +impl AsCoord2d for (T, T) +where + T: Copy + TryInto, + >::Error: Debug, +{ fn to_coord(self) -> Coord2d { Coord2d { - x: self.0.into(), - y: self.1.into(), + x: self.0.try_into().unwrap(), + y: self.1.try_into().unwrap(), } } fn x(&self) -> i64 { - self.0.into() + self.0.try_into().unwrap() } fn y(&self) -> i64 { - self.1.into() - } -} - -impl AsCoord2d for (i64, i64) { - fn to_coord(self) -> Coord2d { - Coord2d { x: self.0, y: self.1 } - } - fn x(&self) -> i64 { - self.0 - } - fn y(&self) -> i64 { - self.1 - } -} - -impl AsCoord2d for (usize, usize) { - fn to_coord(self) -> Coord2d { - Coord2d { - x: self.0 as i64, - y: self.1 as i64, - } - } - fn x(&self) -> i64 { - self.0 as i64 - } - fn y(&self) -> i64 { - self.1 as i64 - } -} - -impl AsCoord2d for (u64, u64) { - fn to_coord(self) -> Coord2d { - Coord2d { - x: self.0 as i64, - y: self.1 as i64, - } - } - fn x(&self) -> i64 { - self.0 as i64 - } - fn y(&self) -> i64 { - self.1 as i64 + self.1.try_into().unwrap() } }