grid: add rotated/mirrored
This commit is contained in:
BIN
.aoc_tiles/tiles/2025/12.png
Normal file
BIN
.aoc_tiles/tiles/2025/12.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.3 KiB |
@@ -1,6 +1,6 @@
|
||||
<!-- AOC TILES BEGIN -->
|
||||
<h1 align="center">
|
||||
2025 - 20 ⭐ - Rust
|
||||
2025 - 21 ⭐ - Rust
|
||||
</h1>
|
||||
<a href="src/day1.rs">
|
||||
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
|
||||
@@ -35,4 +35,7 @@
|
||||
<a href="src/day11.rs">
|
||||
<img src=".aoc_tiles/tiles/2025/11.png" width="161px">
|
||||
</a>
|
||||
<a href="src/day12.rs">
|
||||
<img src=".aoc_tiles/tiles/2025/12.png" width="161px">
|
||||
</a>
|
||||
<!-- AOC TILES END -->
|
||||
|
||||
@@ -22,6 +22,11 @@ pub const ADJACENT_OFFSETS: [&(i64, i64); 8] = [
|
||||
/// NESW
|
||||
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)]
|
||||
pub struct Coord2d {
|
||||
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 data: Vec<T>,
|
||||
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)> {
|
||||
// if (self.width as usize) < needle.len() {
|
||||
// return Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user