day9: part1 and bruteforce part 2 (some cleanups and clippies)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user