day9: part1 and bruteforce part 2 (some cleanups and clippies)

This commit is contained in:
2025-12-08 22:17:10 -08:00
parent 3f9f7afd27
commit 33275b3918
6 changed files with 171 additions and 10 deletions

View File

@@ -22,22 +22,37 @@ pub const ADJACENT_OFFSETS: [&(i64, i64); 8] = [
/// NESW
pub const CARDINAL_OFFSETS: [&(i64, i64); 4] = [&(0, -1), &(-1, 0), &(1, 0), &(0, 1)];
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Coord2d {
pub x: i64,
pub y: i64,
}
impl Debug for Coord2d {
impl Display for Coord2d {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("({}, {})", self.x, self.y))
}
}
impl FromStr for Coord2d {
type Err = Box<dyn std::error::Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (l, r) = s.split_once(',').ok_or("Can't split on ,")?;
Ok(Coord2d {
x: l.parse()?,
y: r.parse()?,
})
}
}
pub trait AsCoord2d {
fn to_coord(self) -> Coord2d;
fn x(&self) -> i64;
fn y(&self) -> i64;
fn manhattan<T: AsCoord2d>(&self, other: &T) -> u64 {
self.x().abs_diff(other.x()) + self.y().abs_diff(other.y())
}
}
impl<T: AsCoord2d> Sub<T> for &Coord2d {