grid: add rotated/mirrored

This commit is contained in:
2025-12-12 00:15:11 -08:00
parent 8cb33180ad
commit d0c14d004f
3 changed files with 83 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2025 - 20 ⭐ - Rust 2025 - 21 ⭐ - Rust
</h1> </h1>
<a href="src/day1.rs"> <a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2025/01.png" width="161px"> <img src=".aoc_tiles/tiles/2025/01.png" width="161px">
@@ -35,4 +35,7 @@
<a href="src/day11.rs"> <a href="src/day11.rs">
<img src=".aoc_tiles/tiles/2025/11.png" width="161px"> <img src=".aoc_tiles/tiles/2025/11.png" width="161px">
</a> </a>
<a href="src/day12.rs">
<img src=".aoc_tiles/tiles/2025/12.png" width="161px">
</a>
<!-- AOC TILES END --> <!-- AOC TILES END -->

View File

@@ -22,6 +22,11 @@ pub const ADJACENT_OFFSETS: [&(i64, i64); 8] = [
/// NESW /// NESW
pub const CARDINAL_OFFSETS: [&(i64, i64); 4] = [&(0, -1), &(-1, 0), &(1, 0), &(0, 1)]; pub const CARDINAL_OFFSETS: [&(i64, i64); 4] = [&(0, -1), &(-1, 0), &(1, 0), &(0, 1)];
pub enum MirrorAxis {
X,
Y,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Coord2d { pub struct Coord2d {
pub x: i64, pub x: i64,
@@ -281,7 +286,7 @@ impl<'a, T: Clone + Eq + PartialEq + Debug> Iterator for OffsetsIter<'a, T> {
} }
} }
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Grid<T> { pub struct Grid<T> {
pub data: Vec<T>, pub data: Vec<T>,
width: i64, width: i64,
@@ -486,6 +491,79 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
} }
} }
pub fn rotated(&self, mut rot: u8) -> Self {
rot %= 4;
match rot {
0 => self.clone(),
1 => {
let mut n = Grid::with_shape(self.height(), self.width(), self.data[0].clone()); // fill will be overwritten anyway
for y in 0..self.height() {
for x in 0..self.width() {
n.set(
&(self.height() - y - 1, x),
self.get(&(x as u64, y as u64)).unwrap().clone(),
);
}
}
n
}
2 => {
let mut n = Grid::with_shape(self.height(), self.width(), self.data[0].clone()); // fill will be overwritten anyway
for y in 0..self.height() {
for x in 0..self.width() {
n.set(
&(self.width() - x - 1, self.height() - y - 1),
self.get(&(x as u64, y as u64)).unwrap().clone(),
);
}
}
n
}
3 => {
let mut n = Grid::with_shape(self.height(), self.width(), self.data[0].clone()); // fill will be overwritten anyway
for y in 0..self.height() {
for x in 0..self.width() {
n.set(
&(self.height() - y - 1, self.width() - x - 1),
self.get(&(x as u64, y as u64)).unwrap().clone(),
);
}
}
n
}
_ => unreachable!("invalid rotation"),
}
}
pub fn mirrored(&self, axis: MirrorAxis) -> Self {
match axis {
MirrorAxis::X => {
let mut n = Grid::with_shape(self.height(), self.width(), self.data[0].clone()); // fill will be overwritten anyway
for y in 0..self.height() {
for x in 0..self.width() {
n.set(
&(self.width() - x - 1, y),
self.get(&(x as u64, y as u64)).unwrap().clone(),
);
}
}
n
}
MirrorAxis::Y => {
let mut n = Grid::with_shape(self.height(), self.width(), self.data[0].clone()); // fill will be overwritten anyway
for y in 0..self.height() {
for x in 0..self.width() {
n.set(
&(x, &self.height() - y - 1),
self.get(&(x as u64, y as u64)).unwrap().clone(),
);
}
}
n
}
}
}
// fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> { // fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> {
// if (self.width as usize) < needle.len() { // if (self.width as usize) < needle.len() {
// return Vec::new(); // return Vec::new();