generated from janezicmatej/aoc-template
	feat: add mul<usize> for direction and diagonal directions
This commit is contained in:
		@@ -22,7 +22,7 @@ fn dijkstra(grid: &Grid, start: Point) -> (Visited, Prev) {
 | 
			
		||||
    let mut visited: Visited = HashMap::new();
 | 
			
		||||
 | 
			
		||||
    let mut bh = BinaryHeap::new();
 | 
			
		||||
    bh.push(Reverse((0, start, Direction::EAST, start, Direction::EAST)));
 | 
			
		||||
    bh.push(Reverse((0, start, Direction::E, start, Direction::E)));
 | 
			
		||||
 | 
			
		||||
    while let Some(Reverse((len, loc, dir, ploc, pdir))) = bh.pop() {
 | 
			
		||||
        let best = visited.entry((loc, dir)).or_insert(usize::MAX);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
use std::fmt::Display;
 | 
			
		||||
use std::{fmt::Display, ops::Mul};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
 | 
			
		||||
pub struct Direction {
 | 
			
		||||
@@ -12,11 +12,33 @@ impl Display for Direction {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Mul<usize> for Direction {
 | 
			
		||||
    type Output = Self;
 | 
			
		||||
    fn mul(self, rhs: usize) -> Self::Output {
 | 
			
		||||
        Self {
 | 
			
		||||
            i: self.i * rhs as isize,
 | 
			
		||||
            j: self.j * rhs as isize,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Direction {
 | 
			
		||||
    pub const NORTH: Self = Direction { i: -1, j: 0 };
 | 
			
		||||
    pub const EAST: Self = Direction { i: 0, j: 1 };
 | 
			
		||||
    pub const SOUTH: Self = Direction { i: 1, j: 0 };
 | 
			
		||||
    pub const WEST: Self = Direction { i: 0, j: -1 };
 | 
			
		||||
    pub const N: Self = Direction { i: -1, j: 0 };
 | 
			
		||||
    pub const E: Self = Direction { i: 0, j: 1 };
 | 
			
		||||
    pub const S: Self = Direction { i: 1, j: 0 };
 | 
			
		||||
    pub const W: Self = Direction { i: 0, j: -1 };
 | 
			
		||||
 | 
			
		||||
    pub const NE: Self = Direction { i: -1, j: 1 };
 | 
			
		||||
    pub const NW: Self = Direction { i: -1, j: -1 };
 | 
			
		||||
    pub const SE: Self = Direction { i: 1, j: 1 };
 | 
			
		||||
    pub const SW: Self = Direction { i: 1, j: -1 };
 | 
			
		||||
 | 
			
		||||
    pub const CROSS: [Self; 4] = [Self::N, Self::E, Self::S, Self::W];
 | 
			
		||||
    #[rustfmt::skip]
 | 
			
		||||
    pub const OMNI: [Self; 8] = [
 | 
			
		||||
        Self::N, Self::E, Self::S, Self::W,
 | 
			
		||||
        Self::NE, Self::NW, Self::SE, Self::SW
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    pub fn new(i: isize, j: isize) -> Self {
 | 
			
		||||
        Self { i, j }
 | 
			
		||||
 
 | 
			
		||||
@@ -81,6 +81,10 @@ impl Point {
 | 
			
		||||
    pub fn sw(self) -> Self {
 | 
			
		||||
        self.s().w()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn bounded_by(&self, other: &Self) -> bool {
 | 
			
		||||
        self.i <= other.i && self.j <= other.j
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[macro_export]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user