grid improvements

This commit is contained in:
2025-12-03 22:07:36 -08:00
parent 4b51bcac08
commit 6acb491b45
3 changed files with 39 additions and 9 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2025 - 7 ⭐ - Rust 2025 - 8 ⭐ - 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">

View File

@@ -149,7 +149,34 @@ impl<'a, T: Clone + Eq + PartialEq + Debug> Iterator for CoordIter<'a, T> {
type Item = Coord2d; type Item = Coord2d;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.grid.data.len() { if self.pos < self.grid.data.len() {
self.grid.coord(self.pos as i64).into() self.pos += 1;
self.grid.coord(self.pos as i64 - 1).into()
} else {
None
}
}
}
#[derive(Debug)]
pub struct ItemIter<'a, T> {
pos: usize,
grid: &'a Grid<T>,
}
pub struct Item<'a, T> {
pub pos: Coord2d,
pub value: &'a T,
}
impl<'a, T: Clone + Eq + PartialEq + Debug> Iterator for ItemIter<'a, T> {
type Item = Item<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.grid.data.len() {
self.pos += 1;
Some(Item {
pos: self.grid.coord(self.pos as i64 - 1).unwrap(),
value: &self.grid.data[self.pos - 1],
})
} else { } else {
None None
} }
@@ -226,9 +253,12 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
}) })
} }
} }
// pub fn coord_iter(&self) -> CoordIter<_> { pub fn coord_iter<'a>(&'a self) -> CoordIter<'a, T> {
// CoordIter { pos: 0, grid: self } CoordIter { pos: 0, grid: self }
// } }
pub fn item_iter<'a>(&'a self) -> ItemIter<'a, T> {
ItemIter { pos: 0, grid: self }
}
pub fn is_valid<C: AsCoord2d>(&self, c: &C) -> bool { pub fn is_valid<C: AsCoord2d>(&self, c: &C) -> bool {
if c.x() < 0 || c.x() >= self.width { if c.x() < 0 || c.x() >= self.width {
return false; return false;
@@ -292,7 +322,7 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
} }
} }
pub fn row_iter(&self, y: i64) -> Option<GridRowIter<T>> { pub fn row_iter<'a>(&'a self, y: i64) -> Option<GridRowIter<'a, T>> {
if (y as usize) < self.height() { if (y as usize) < self.height() {
Some(GridRowIter::new(self, y)) Some(GridRowIter::new(self, y))
} else { } else {
@@ -308,7 +338,7 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
} }
} }
pub fn col_iter(&self, x: i64) -> Option<GridColIter<T>> { pub fn col_iter<'a>(&'a self, x: i64) -> Option<GridColIter<'a, T>> {
if (x as usize) < self.width() { if (x as usize) < self.width() {
Some(GridColIter::new(self, x)) Some(GridColIter::new(self, x))
} else { } else {
@@ -316,13 +346,13 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
} }
} }
pub fn find(&self, haystack: &T) -> Option<Coord2d> { pub fn find(&self, needle: &T) -> Option<Coord2d> {
self.coord( self.coord(
self.data self.data
.iter() .iter()
.enumerate() .enumerate()
.find_map(|(pos, val)| { .find_map(|(pos, val)| {
if val == haystack { if val == needle {
Some(pos as i64) Some(pos as i64)
} else { } else {
None None