Skip to content

Commit

Permalink
solve: day12
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Dec 12, 2024
1 parent 7eb1f1b commit b3df4b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"utils",
"aoc_derive", "day1", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9", "day10", "day11",
"aoc_derive", "day1", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9", "day10", "day11", "day12",
]

[workspace.dependencies]
Expand Down
9 changes: 9 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ pub trait EvenMoreItertools: Iterator {
{
self.enumerate().filter_map(move |(i, x)| (i != skip).then_some(x))
}

fn unzip_vec<A, B>(self) -> (Vec<A>, Vec<B>)
where
Self: Sized + Iterator<Item = (A, B)>,
{
let mut unzipped: (Vec<A>, Vec<B>) = Default::default();
unzipped.extend(self);
unzipped
}
}

impl<T: ?Sized> EvenMoreItertools for T where T: Iterator {}
Expand Down
75 changes: 34 additions & 41 deletions utils/src/math/vec2d.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use parse_display::FromStr;

#[derive(Copy, Clone, Debug, Eq, Hash, FromStr, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Eq, Hash, FromStr, PartialOrd, Ord, derive_more::Display)]
#[display("({x}, {y})")]
// Parses [1, 2] or (1, 2) or {1, 2}
#[from_str(regex = r"[\[\(\{](?<x>-?\d+),\s*(?<y>-?\d+)[\]\)\}]")]
pub struct Vec2D {
pub x: i64,
pub y: i64,
}

impl<T> PartialEq<T> for Vec2D where T: Into<Vec2D> + Copy {
impl<T> PartialEq<T> for Vec2D
where
T: Into<Vec2D> + Copy,
{
fn eq(&self, other: &T) -> bool {
let other: Vec2D = (*other).into();
self.x == other.x && self.y == other.y
Expand Down Expand Up @@ -70,25 +74,15 @@ impl Vec2D {
}

pub fn diagonal_neighbors(&self) -> impl Iterator<Item = Vec2D> + '_ {
[
Vec2D { x: 1, y: 1 },
Vec2D { x: -1, y: 1 },
Vec2D { x: 1, y: -1 },
Vec2D { x: -1, y: -1 },
]
.iter()
.map(move |&dir| *self + dir)
[Vec2D { x: 1, y: 1 }, Vec2D { x: -1, y: 1 }, Vec2D { x: 1, y: -1 }, Vec2D { x: -1, y: -1 }]
.iter()
.map(move |&dir| *self + dir)
}

pub fn orthogonal_neighbors(&self) -> impl Iterator<Item = Vec2D> + '_ {
[
Vec2D { x: 1, y: 0 },
Vec2D { x: -1, y: 0 },
Vec2D { x: 0, y: 1 },
Vec2D { x: 0, y: -1 },
]
.iter()
.map(move |&dir| *self + dir)
[Vec2D { x: 1, y: 0 }, Vec2D { x: -1, y: 0 }, Vec2D { x: 0, y: 1 }, Vec2D { x: 0, y: -1 }]
.iter()
.map(move |&dir| *self + dir)
}

pub fn all_neighbors(&self) -> impl Iterator<Item = Vec2D> + '_ {
Expand Down Expand Up @@ -128,14 +122,11 @@ where
type Output = Vec2D;

fn mul(self, rhs: T) -> Self::Output {
Vec2D::new(
self.x * rhs.to_i64().unwrap(),
self.y * rhs.to_i64().unwrap(),
)
Vec2D::new(self.x * rhs.to_i64().unwrap(), self.y * rhs.to_i64().unwrap())
}
}

impl <T> std::ops::MulAssign<T> for Vec2D
impl<T> std::ops::MulAssign<T> for Vec2D
where
T: num::ToPrimitive + num::Integer,
{
Expand All @@ -151,7 +142,7 @@ macro_rules! impl_left_mul {
$(
impl std::ops::Mul<Vec2D> for $t {
type Output = Vec2D;

fn mul(self, rhs: Vec2D) -> Self::Output {
rhs * self
}
Expand All @@ -161,7 +152,10 @@ macro_rules! impl_left_mul {
}
impl_left_mul!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);

impl<T> std::ops::Add<T> for Vec2D where T: Into<Vec2D> {
impl<T> std::ops::Add<T> for Vec2D
where
T: Into<Vec2D>,
{
type Output = Vec2D;

fn add(self, rhs: T) -> Self::Output {
Expand All @@ -170,13 +164,19 @@ impl<T> std::ops::Add<T> for Vec2D where T: Into<Vec2D> {
}
}

impl<T> std::ops::AddAssign<T> for Vec2D where T: Into<Vec2D> {
impl<T> std::ops::AddAssign<T> for Vec2D
where
T: Into<Vec2D>,
{
fn add_assign(&mut self, rhs: T) {
*self = *self + rhs;
}
}

impl<T> std::ops::Sub<T> for Vec2D where T: Into<Vec2D> {
impl<T> std::ops::Sub<T> for Vec2D
where
T: Into<Vec2D>,
{
type Output = Vec2D;

fn sub(self, rhs: T) -> Self::Output {
Expand All @@ -185,7 +185,10 @@ impl<T> std::ops::Sub<T> for Vec2D where T: Into<Vec2D> {
}
}

impl<T> std::ops::SubAssign<T> for Vec2D where T: Into<Vec2D> {
impl<T> std::ops::SubAssign<T> for Vec2D
where
T: Into<Vec2D>,
{
fn sub_assign(&mut self, rhs: T) {
*self = *self - rhs;
}
Expand Down Expand Up @@ -246,7 +249,7 @@ mod tests {
assert_eq!(Vec2D::new(1, -2).manhattan_dist(), 3);
}

#[test]
#[test]
fn inside_box() {
assert!(Vec2D::new(1, 2).inside_box((0, 0), (2, 3)));
assert!(Vec2D::new(1, 2).inside_box((1, 2), (1, 2)));
Expand Down Expand Up @@ -275,21 +278,11 @@ mod tests {
#[test]
fn neighbors() {
assert_eq!(
HashSet::from([
Vec2D::new(2, 2),
Vec2D::new(0, 2),
Vec2D::new(1, 3),
Vec2D::new(1, 1)
]),
HashSet::from([Vec2D::new(2, 2), Vec2D::new(0, 2), Vec2D::new(1, 3), Vec2D::new(1, 1)]),
Vec2D::new(1, 2).orthogonal_neighbors().collect(),
);
assert_eq!(
HashSet::from([
Vec2D::new(2, 3),
Vec2D::new(0, 3),
Vec2D::new(2, 1),
Vec2D::new(0, 1)
]),
HashSet::from([Vec2D::new(2, 3), Vec2D::new(0, 3), Vec2D::new(2, 1), Vec2D::new(0, 1)]),
Vec2D::new(1, 2).diagonal_neighbors().collect(),
);
assert_eq!(
Expand Down

0 comments on commit b3df4b9

Please sign in to comment.