day15: part 1 solution + beginning of part 2
Some checks failed
test / AoC 2024 (push) Has been cancelled

This commit is contained in:
2024-12-14 22:49:21 -08:00
parent 003bc3212d
commit 4dfdaca58c
5 changed files with 314 additions and 3 deletions

View File

@ -152,7 +152,7 @@ impl<T: Clone + Eq + PartialEq + Display + Debug> Grid<T> {
pub fn height(&self) -> usize {
self.data.len() / self.width()
}
fn pos<C: AsCoord2d>(&self, c: &C) -> i64 {
pub fn pos<C: AsCoord2d>(&self, c: &C) -> i64 {
c.y() * self.width + c.x()
}
pub fn coord(&self, pos: i64) -> Option<(i64, i64)> {
@ -203,13 +203,21 @@ impl<T: Clone + Eq + PartialEq + Display + Debug> Grid<T> {
}
}
pub fn row(&self, y: i64) -> Option<&[T]> {
if y < self.height() as i64 {
if y < self.height() as i64 && y >= 0 {
Some(&self.data[self.pos(&(0, y)) as usize..self.pos(&(self.width, y)) as usize])
} else {
None
}
}
pub fn col(&self, x: i64) -> Option<Vec<&T>> {
if x < self.width() as i64 && x >= 0 {
Some((0..self.height()).map(|y| self.get(&(x, y as i64)).unwrap()).collect())
} else {
None
}
}
pub fn find(&self, haystack: &T) -> Option<(i64, i64)> {
self.coord(
self.data
@ -231,6 +239,13 @@ impl<T: Clone + Eq + PartialEq + Display + Debug> Grid<T> {
}
}
pub fn swap<A: AsCoord2d, B: AsCoord2d>(&mut self, a: A, b: B) {
match (self.valid_pos(&a), self.valid_pos(&b)) {
(Some(a), Some(b)) => self.data.swap(a, b),
_ => {}
}
}
// fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> {
// if (self.width as usize) < needle.len() {
// return Vec::new();