day14: complete solution
Some checks failed
test / AoC 2024 (push) Failing after 58s

This commit is contained in:
2024-12-13 22:53:00 -08:00
parent 8b011941c4
commit c6153663b5
5 changed files with 209 additions and 7 deletions

View File

@ -3,7 +3,7 @@ use std::{
io::{BufRead, Cursor},
iter::repeat,
mem::swap,
ops::{Add, Sub},
ops::{Add, AddAssign, Sub},
str::FromStr,
};
@ -51,6 +51,18 @@ impl AsCoord2d for Coord2d {
}
}
impl AsCoord2d for &Coord2d {
fn to_coord(self) -> Coord2d {
self.to_owned()
}
fn x(&self) -> i64 {
self.x
}
fn y(&self) -> i64 {
self.y
}
}
impl AsCoord2d for (i32, i32) {
fn to_coord(self) -> Coord2d {
Coord2d {
@ -178,6 +190,18 @@ impl<T: Clone + Eq + PartialEq + Display + Debug> Grid<T> {
None => None,
}
}
pub fn increment<'a, A, C: AsCoord2d>(&'a mut self, c: &C, i: A) -> Option<&'a T>
where
T: AddAssign<A>,
{
match self.valid_pos(c) {
Some(pos) => {
self.data[pos] += i;
Some(&self.data[pos])
}
None => None,
}
}
pub fn row(&self, y: i64) -> Option<&[T]> {
if y < self.height() as i64 {
Some(&self.data[self.pos(&(0, y)) as usize..self.pos(&(self.width, y)) as usize])
@ -300,7 +324,10 @@ FBCG";
fn from_string() {
let grid = unchecked_load();
assert_eq!(grid.data, "ABCDEFGHIJKLFBCG".as_bytes());
assert_eq!(TEST_VECTOR_S.parse::<Grid<u8>>().unwrap().data, "ABCDEFGHIJKLFBCG".as_bytes());
assert_eq!(
TEST_VECTOR_S.parse::<Grid<u8>>().unwrap().data,
"ABCDEFGHIJKLFBCG".as_bytes()
);
}
#[test]