grid improvements
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 7.1 KiB |
@@ -1,6 +1,6 @@
|
||||
<!-- AOC TILES BEGIN -->
|
||||
<h1 align="center">
|
||||
2025 - 7 ⭐ - Rust
|
||||
2025 - 8 ⭐ - Rust
|
||||
</h1>
|
||||
<a href="src/day1.rs">
|
||||
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
|
||||
|
||||
@@ -149,7 +149,34 @@ impl<'a, T: Clone + Eq + PartialEq + Debug> Iterator for CoordIter<'a, T> {
|
||||
type Item = Coord2d;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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 {
|
||||
None
|
||||
}
|
||||
@@ -226,9 +253,12 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
|
||||
})
|
||||
}
|
||||
}
|
||||
// pub fn coord_iter(&self) -> CoordIter<_> {
|
||||
// CoordIter { pos: 0, grid: self }
|
||||
// }
|
||||
pub fn coord_iter<'a>(&'a self) -> CoordIter<'a, T> {
|
||||
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 {
|
||||
if c.x() < 0 || c.x() >= self.width {
|
||||
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() {
|
||||
Some(GridRowIter::new(self, y))
|
||||
} 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() {
|
||||
Some(GridColIter::new(self, x))
|
||||
} 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.data
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(pos, val)| {
|
||||
if val == haystack {
|
||||
if val == needle {
|
||||
Some(pos as i64)
|
||||
} else {
|
||||
None
|
||||
|
||||
Reference in New Issue
Block a user