day16: improve perf again by going to i16 positions
All checks were successful
test / AoC 2024 (push) Successful in 5m29s
All checks were successful
test / AoC 2024 (push) Successful in 5m29s
map dimensions fit in i16, so use those in data structures make grid support generic position type as long as they implement to i64
This commit is contained in:
parent
755fbbc53d
commit
20e6889572
17
src/day16.rs
17
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<Coord>,
|
||||
}
|
||||
|
||||
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<Vec<(i32, i32)>>) {
|
||||
fn path_dijkstra(&mut self) -> (usize, Vec<Vec<Coord>>) {
|
||||
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");
|
||||
|
@ -63,60 +63,22 @@ impl AsCoord2d for &Coord2d {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsCoord2d for (i32, i32) {
|
||||
impl<T> AsCoord2d for (T, T)
|
||||
where
|
||||
T: Copy + TryInto<i64>,
|
||||
<T as TryInto<i64>>::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()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user