|
| 1 | +use std::{ |
| 2 | + fmt::{Display, Formatter}, |
| 3 | + ops::{Index, IndexMut}, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::point::Point; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Hash, Eq, PartialEq)] |
| 9 | +pub struct Grid<T> { |
| 10 | + pub width: i32, |
| 11 | + pub height: i32, |
| 12 | + pub data: Vec<T>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<T: From<u8> + Copy> From<&str> for Grid<T> { |
| 16 | + fn from(value: &str) -> Self { |
| 17 | + let raw_data = value |
| 18 | + .lines() |
| 19 | + .flat_map(|line| line.bytes().filter_map(|b| T::from(b).into())) |
| 20 | + .collect::<Vec<_>>(); |
| 21 | + let width = value.lines().next().unwrap_or_default().len() as i32; |
| 22 | + let height = value.lines().count() as i32; |
| 23 | + |
| 24 | + Grid { |
| 25 | + width, |
| 26 | + height, |
| 27 | + data: raw_data, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl<T: Copy + PartialEq> Grid<T> { |
| 33 | + pub fn new(width: i32, height: i32, data: Vec<T>) -> Self { |
| 34 | + Grid { |
| 35 | + width, |
| 36 | + height, |
| 37 | + data, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Find _one_ point that has the given value in the grid. |
| 42 | + /// |
| 43 | + /// # Example |
| 44 | + /// |
| 45 | + /// ``` |
| 46 | + /// use advent_of_code_2023::grid::Grid; |
| 47 | + /// use advent_of_code_2023::point::Point; |
| 48 | + /// |
| 49 | + /// let data = "...# |
| 50 | + /// .#S. |
| 51 | + /// ..#."; |
| 52 | + /// |
| 53 | + /// let grid: Grid<u8> = Grid::from(data); |
| 54 | + /// let point = grid.find(b'S'); |
| 55 | + /// |
| 56 | + /// assert_eq!(point, Some(Point { x: 2, y: 1 })) |
| 57 | + /// ``` |
| 58 | + pub fn find(&self, value: T) -> Option<Point> { |
| 59 | + self.data |
| 60 | + .iter() |
| 61 | + .position(|&x| x == value) |
| 62 | + .map(|i| Point::new((i as i32) % self.width, (i as i32) / self.width)) |
| 63 | + } |
| 64 | + |
| 65 | + /// Find all points that contain the given value in the grid. |
| 66 | + /// |
| 67 | + /// # Example |
| 68 | + /// |
| 69 | + /// ``` |
| 70 | + /// use advent_of_code_2023::grid::Grid; |
| 71 | + /// |
| 72 | + /// let data = "...# |
| 73 | + /// .#.. |
| 74 | + /// ..#."; |
| 75 | + /// |
| 76 | + /// let grid: Grid<u8> = Grid::from(data); |
| 77 | + /// let points = grid.find_all(b'#'); |
| 78 | + /// |
| 79 | + /// assert_eq!(points.len(), 3); |
| 80 | + /// ``` |
| 81 | + pub fn find_all(&self, value: T) -> Vec<Point> { |
| 82 | + self.data |
| 83 | + .iter() |
| 84 | + .enumerate() |
| 85 | + .filter_map(|(i, &x)| if x == value { Some(i) } else { None }) |
| 86 | + .map(|i| Point::new((i as i32) % self.width, (i as i32) / self.width)) |
| 87 | + .collect() |
| 88 | + } |
| 89 | + |
| 90 | + /// Find if the grid contains a point |
| 91 | + /// |
| 92 | + /// # Example |
| 93 | + /// |
| 94 | + /// ``` |
| 95 | + /// use advent_of_code_2023::grid::Grid; |
| 96 | + /// use advent_of_code_2023::point::Point; |
| 97 | + /// |
| 98 | + /// let data = "...# |
| 99 | + /// .#.. |
| 100 | + /// ..#."; |
| 101 | + /// |
| 102 | + /// let grid: Grid<u8> = Grid::from(data); |
| 103 | + /// |
| 104 | + /// let existing_point = Point::new(2, 1); |
| 105 | + /// let non_existing_point = Point::new(3, 3); |
| 106 | + /// |
| 107 | + /// assert!(grid.contains(existing_point)); |
| 108 | + /// assert!(!grid.contains(non_existing_point)); |
| 109 | + /// ``` |
| 110 | + pub fn contains(&self, point: Point) -> bool { |
| 111 | + point.x >= 0 && point.x < self.width && point.y >= 0 && point.y < self.height |
| 112 | + } |
| 113 | + |
| 114 | + /// Swap the values of two points in the grid |
| 115 | + /// |
| 116 | + /// # Example |
| 117 | + /// |
| 118 | + /// ``` |
| 119 | + /// use advent_of_code_2023::grid::Grid; |
| 120 | + /// use advent_of_code_2023::point::Point; |
| 121 | + /// |
| 122 | + /// let data = "...# |
| 123 | + /// .#.. |
| 124 | + /// ..#."; |
| 125 | + /// |
| 126 | + /// let mut grid: Grid<u8> = Grid::from(data); |
| 127 | + /// |
| 128 | + /// let point_a = Point::new(3, 0); |
| 129 | + /// let point_b = Point::new(2, 0); |
| 130 | + /// |
| 131 | + /// assert_eq!(grid[point_a], b'#'); |
| 132 | + /// assert_eq!(grid[point_b], b'.'); |
| 133 | + /// |
| 134 | + /// grid.swap(point_a, point_b); |
| 135 | + /// |
| 136 | + /// assert_eq!(grid[point_a], b'.'); |
| 137 | + /// assert_eq!(grid[point_b], b'#'); |
| 138 | + /// ``` |
| 139 | + pub fn swap(&mut self, a: Point, b: Point) { |
| 140 | + let a = (self.width * a.y + a.x) as usize; |
| 141 | + let b = (self.width * b.y + b.x) as usize; |
| 142 | + |
| 143 | + self.data.swap(a, b); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/// Used to get a value using a Point as index from the grid |
| 148 | +/// |
| 149 | +/// ``` |
| 150 | +/// use advent_of_code_2023::grid::Grid; |
| 151 | +/// use advent_of_code_2023::point::Point; |
| 152 | +/// |
| 153 | +/// let grid: Grid<u8> = Grid::from(".#.."); |
| 154 | +/// assert_eq!(grid[Point::new(1, 0)], b'#'); |
| 155 | +/// ``` |
| 156 | +impl<T> Index<Point> for Grid<T> { |
| 157 | + type Output = T; |
| 158 | + |
| 159 | + #[inline] |
| 160 | + fn index(&self, point: Point) -> &Self::Output { |
| 161 | + &self.data[(self.width * point.y + point.x) as usize] |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/// Used to set a value using a Point as index on the grid |
| 166 | +/// |
| 167 | +/// ``` |
| 168 | +/// use advent_of_code_2023::grid::Grid; |
| 169 | +/// use advent_of_code_2023::point::Point; |
| 170 | +/// |
| 171 | +/// let mut grid: Grid<u8> = Grid::from("...."); |
| 172 | +/// let point = Point::new(1,0); |
| 173 | +/// |
| 174 | +/// grid[point] = b'#'; |
| 175 | +/// |
| 176 | +/// assert_eq!(grid[point], b'#'); |
| 177 | +/// ``` |
| 178 | +impl<T> IndexMut<Point> for Grid<T> { |
| 179 | + fn index_mut(&mut self, point: Point) -> &mut Self::Output { |
| 180 | + &mut self.data[(self.width * point.y + point.x) as usize] |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +/// Used for debugging a grid visually. |
| 185 | +impl Display for Grid<u8> { |
| 186 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 187 | + for y in 0..self.height { |
| 188 | + for x in 0..self.width { |
| 189 | + write!(f, "{}", self[Point::new(x, y)] as char)?; |
| 190 | + } |
| 191 | + writeln!(f)?; |
| 192 | + } |
| 193 | + |
| 194 | + Ok(()) |
| 195 | + } |
| 196 | +} |
0 commit comments