|
| 1 | +use crate::grid::{Grid, Pos}; |
| 2 | + |
| 3 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 4 | +pub struct DirectedPos { |
| 5 | + pos: Pos, |
| 6 | + dir: Direction, |
| 7 | +} |
| 8 | + |
| 9 | +#[repr(u8)] |
| 10 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | +pub enum Direction { |
| 12 | + North = 0, |
| 13 | + NorthEast = 1, |
| 14 | + East = 2, |
| 15 | + SouthEast = 3, |
| 16 | + South = 4, |
| 17 | + SouthWest = 5, |
| 18 | + West = 6, |
| 19 | + NorthWest = 7, |
| 20 | +} |
| 21 | + |
| 22 | +impl DirectedPos { |
| 23 | + pub fn new(pos: Pos, dir: Direction) -> Self { |
| 24 | + Self { pos, dir } |
| 25 | + } |
| 26 | + |
| 27 | + pub fn pos(&self) -> Pos { |
| 28 | + self.pos |
| 29 | + } |
| 30 | + |
| 31 | + pub fn direction(&self) -> Direction { |
| 32 | + self.dir |
| 33 | + } |
| 34 | + |
| 35 | + pub fn index(&self) -> usize { |
| 36 | + self.pos.index() |
| 37 | + } |
| 38 | + |
| 39 | + pub fn value(&self) -> char { |
| 40 | + self.pos.value() |
| 41 | + } |
| 42 | + |
| 43 | + pub fn row(&self) -> usize { |
| 44 | + self.pos.row() |
| 45 | + } |
| 46 | + |
| 47 | + pub fn col(&self) -> usize { |
| 48 | + self.pos.col() |
| 49 | + } |
| 50 | + |
| 51 | + pub fn north(&self, grid: &Grid) -> Option<DirectedPos> { |
| 52 | + self.pos.north(grid).map(|p| Self::new(p, self.dir)) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn north_east(&self, grid: &Grid) -> Option<DirectedPos> { |
| 56 | + self.pos.north_east(grid).map(|p| Self::new(p, self.dir)) |
| 57 | + } |
| 58 | + |
| 59 | + pub fn east(&self, grid: &Grid) -> Option<DirectedPos> { |
| 60 | + self.pos.east(grid).map(|p| Self::new(p, self.dir)) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn south_east(&self, grid: &Grid) -> Option<DirectedPos> { |
| 64 | + self.pos.south_east(grid).map(|p| Self::new(p, self.dir)) |
| 65 | + } |
| 66 | + |
| 67 | + pub fn south(&self, grid: &Grid) -> Option<DirectedPos> { |
| 68 | + self.pos.south(grid).map(|p| Self::new(p, self.dir)) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn south_west(&self, grid: &Grid) -> Option<DirectedPos> { |
| 72 | + self.pos.south_west(grid).map(|p| Self::new(p, self.dir)) |
| 73 | + } |
| 74 | + |
| 75 | + pub fn west(&self, grid: &Grid) -> Option<DirectedPos> { |
| 76 | + self.pos.west(grid).map(|p| Self::new(p, self.dir)) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn north_west(&self, grid: &Grid) -> Option<DirectedPos> { |
| 80 | + self.pos.north_west(grid).map(|p| Self::new(p, self.dir)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod test { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_pos() { |
| 90 | + let grid = Grid::new("012\n345\n678".to_string()); |
| 91 | + let pos = DirectedPos::new(grid.pos(4), Direction::North); |
| 92 | + assert_eq!(pos, grid.pos(4).directed(Direction::North)); |
| 93 | + assert_eq!(pos.direction(), Direction::North); |
| 94 | + assert_eq!(pos.index(), 4); |
| 95 | + assert_eq!( |
| 96 | + pos.north(&grid).unwrap(), |
| 97 | + DirectedPos::new(grid.pos(1), Direction::North) |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments