diff options
author | mhsn <mail@mhsn.net> | 2025-08-07 21:35:55 +0100 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2025-08-07 21:35:55 +0100 |
commit | 10bb2a67c5bb005b304508eb2026780531132f82 (patch) | |
tree | 4e4f72ea6a1fee203fa6c34ed6cc5b570d507100 /lib | |
parent | 81232bba7f13c0cb2929f8ab44b789020ccb978f (diff) | |
download | aoc-10bb2a67c5bb005b304508eb2026780531132f82.tar.gz aoc-10bb2a67c5bb005b304508eb2026780531132f82.zip |
2024-06 rust p1
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rust/src/grid.rs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/rust/src/grid.rs b/lib/rust/src/grid.rs index 1b43852..671c7de 100644 --- a/lib/rust/src/grid.rs +++ b/lib/rust/src/grid.rs @@ -9,8 +9,8 @@ type Point = (usize, usize); #[derive(PartialEq, Eq, Clone, Hash, Debug, Copy)] pub struct Direction { - x: i64, - y: i64, + pub x: i64, + pub y: i64, } impl From<(i64, i64)> for Direction { @@ -20,12 +20,12 @@ impl From<(i64, i64)> for Direction { } impl Direction { - fn cw(self) -> Self { - (self.y, -self.x).into() - } - fn acw(self) -> Self { + pub fn cw(self) -> Self { (-self.y, self.x).into() } + pub fn acw(self) -> Self { + (self.y, -self.x).into() + } } pub fn chebyshev_ball(n: i64) -> impl Iterator<Item = Direction> { @@ -58,6 +58,10 @@ impl<T> Grid<T> { (0..self.width).flat_map(|x| (0..self.height).map(move |y| (x, y))) } + pub fn enumerate(&self) -> impl Iterator<Item = (Point, &T)> { + self.points().map(|p| (p, self.at(p).unwrap())) + } + pub fn move_pos(&self, (x, y): Point, d: Direction, n: i64) -> Option<Point> { let x = (i64::try_from(x).ok()? + n * d.x).try_into().ok()?; let y = (i64::try_from(y).ok()? + n * d.y).try_into().ok()?; |