From 123d9bd1fb1da8c1e7ff4898ce09982c181e8d5e Mon Sep 17 00:00:00 2001 From: kageru Date: Thu, 8 Jul 2021 19:35:31 +0200 Subject: [PATCH] Get rid of Position2D, 3D, 4D --- 2020/src/bin/day11.rs | 6 +- 2020/src/bin/day17.rs | 5 +- 2020/src/grid.rs | 6 +- 2020/src/grid/position.rs | 195 ++------------------------------------ 4 files changed, 18 insertions(+), 194 deletions(-) diff --git a/2020/src/bin/day11.rs b/2020/src/bin/day11.rs index 4dfe377..71f4972 100644 --- a/2020/src/bin/day11.rs +++ b/2020/src/bin/day11.rs @@ -66,7 +66,7 @@ fn parse_input(raw: &str) -> Parsed { fn occupied_neighbors(pos: &PositionND<2>, grid: &Parsed) -> usize { pos.neighbors() .iter() - .filter(|p| grid.get(&p).unwrap_or(&Tile::Floor) == &Tile::Occupied) + .filter(|p| grid.get(p).unwrap_or(&Tile::Floor) == &Tile::Occupied) .count() } @@ -96,13 +96,13 @@ fn make_step, &Parsed) -> usize>(previous: &mut Parsed, cou match tile { Tile::Floor => (), Tile::Empty => { - if count_neighbors(&pos, &readonly) == 0 { + if count_neighbors(pos, &readonly) == 0 { *tile = Tile::Occupied; changed = true; } } Tile::Occupied => { - if count_neighbors(&pos, &readonly) >= limit { + if count_neighbors(pos, &readonly) >= limit { *tile = Tile::Empty; changed = true; } diff --git a/2020/src/bin/day17.rs b/2020/src/bin/day17.rs index 48f01de..df8f7ac 100644 --- a/2020/src/bin/day17.rs +++ b/2020/src/bin/day17.rs @@ -45,12 +45,11 @@ where [(); grid::num_neighbors(D)]: Sized { fn next_state(pos: &PositionND, grid: &Grid) -> Cell where [(); grid::num_neighbors(D)]: Sized { let cell = grid.get(pos); - let new = match (&cell, count_live_neighbors::(pos, &grid)) { + match (&cell, count_live_neighbors::(pos, grid)) { (Cell::Alive, 2..=3) => Cell::Alive, (Cell::Dead, 3) => Cell::Alive, _ => Cell::Dead, - }; - new + } } fn solve(parsed: &Grid, steps: usize) -> usize diff --git a/2020/src/grid.rs b/2020/src/grid.rs index 325b805..621eb8a 100644 --- a/2020/src/grid.rs +++ b/2020/src/grid.rs @@ -14,7 +14,7 @@ pub struct Grid { impl Grid { pub fn get(&self, pos: &PositionND) -> T { - self.fields.get(pos).copied().unwrap_or_else(|| T::default()) + self.fields.get(pos).copied().unwrap_or_else(T::default) } pub fn insert>>(&mut self, pos: Pos, t: T) { @@ -63,7 +63,7 @@ mod tests { use super::*; #[test] fn test_add() { - assert_eq!(Position2D { x: 0, y: 2 } + Position2D { x: -1, y: 0 }, (-1, 2).into()); - assert_eq!(Position2D { x: 0, y: -1 } + Direction::Up, (0, 0).into()); + assert_eq!(PositionND::from([0, 2]) + PositionND::from([-1, 0]), [-1, 2].into()); + assert_eq!(PositionND::from([0, -1]) + PositionND::from(Direction::Up), [0, 0].into()); } } diff --git a/2020/src/grid/position.rs b/2020/src/grid/position.rs index 8c4eb46..ef5e17f 100644 --- a/2020/src/grid/position.rs +++ b/2020/src/grid/position.rs @@ -1,9 +1,8 @@ extern crate test; use super::direction::*; -use impl_ops::*; use itertools::iproduct; use std::{ - convert::TryInto, hash::Hash, ops::{self, Add, AddAssign, Mul, Sub} + convert::TryInto, hash::Hash, ops::{Add, Mul, Sub} }; pub trait Position @@ -12,27 +11,6 @@ where Self: Sized + Hash + PartialEq + Eq + Clone + Copy fn neighbors(&self) -> Vec; } -#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)] -pub struct Position2D { - pub x: i64, - pub y: i64, -} - -#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)] -pub struct Position3D { - pub x: i64, - pub y: i64, - pub z: i64, -} - -#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)] -pub struct Position4D { - pub x: i64, - pub y: i64, - pub z: i64, - pub w: i64, -} - #[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)] pub struct PositionND { pub points: [i64; DIMS], @@ -50,6 +28,15 @@ where I: TryInto + Copy } } +// because calling .unwrap() on a TryInto result isn’t possible without trait bounds on the +// associated Error type. +fn unwrap_number_result>(i: I) -> i64 { + match i.try_into() { + Ok(i) => i, + _ => panic!("Bad coordinate"), + } +} + pub const fn num_neighbors(d: usize) -> usize { 3usize.pow(d as u32) - 1 } @@ -174,168 +161,6 @@ impl From for PositionND<2> { } } -mod p2d { - use super::*; - - impl From for Position2D { - fn from(d: Direction) -> Self { - match d { - Direction::Up => Position2D::from((0, 1)), - Direction::Right => Position2D::from((1, 0)), - Direction::Left => Position2D::from((-1, 0)), - Direction::Down => Position2D::from((0, -1)), - } - } - } - - impl Position for Position2D { - fn neighbors(&self) -> Vec { - vec![ - *self + Direction::Up + Direction::Left, - *self + Direction::Up, - *self + Direction::Up + Direction::Right, - *self + Direction::Left, - *self + Direction::Right, - *self + Direction::Down + Direction::Left, - *self + Direction::Down, - *self + Direction::Down + Direction::Right, - ] - } - } - - impl> From<(I, I)> for Position2D { - fn from((x, y): (I, I)) -> Position2D { - Position2D { x: x.into(), y: y.into() } - } - } - - impl_op!(+|a: Position2D, b: Direction| -> Position2D { a + Position2D::from(b) }); - - impl_op!(-|a: Position2D, b: Direction| -> Position2D { a - Position2D::from(b) }); - - impl_op!(*|a: Position2D, b: i64| -> Position2D { Position2D { x: a.x * b, y: a.y * b } }); - - impl_op!(+|a: Position2D, b: Position2D| -> Position2D { - Position2D { - x: a.x + b.x, - y: a.y + b.y - } - }); - - impl_op!(-|a: Position2D, b: Position2D| -> Position2D { - Position2D { - x: a.x - b.x, - y: a.y - b.y, - } - }); - - impl AddAssign for Position2D { - fn add_assign(&mut self, rhs: Direction) { - *self = *self + rhs; - } - } - - impl AddAssign for Position2D { - fn add_assign(&mut self, rhs: Position2D) { - *self = *self + rhs; - } - } -} - -mod p3d { - use super::*; - - impl Position for Position3D { - fn neighbors(&self) -> Vec { - iproduct!((-1..=1), (-1..=1), (-1..=1)) - .filter(|t| t != &(0, 0, 0)) - .map(|(x, y, z)| *self + Position3D::from((x, y, z))) - .collect() - } - } - - impl From<(I, I, I)> for Position3D - where I: TryInto - { - fn from((x, y, z): (I, I, I)) -> Position3D { - Position3D { - x: unwrap_number_result(x), - y: unwrap_number_result(y), - z: unwrap_number_result(z), - } - } - } - - impl_op!(-|a: Position3D, b: Position3D| -> Position3D { - Position3D { - x: a.x - b.x, - y: a.y - b.y, - z: a.z - b.z, - } - }); - - impl_op!(+|a: Position3D, b: Position3D| -> Position3D { - Position3D { - x: a.x + b.x, - y: a.y + b.y, - z: a.z + b.z, - } - }); -} - -mod p4d { - use super::*; - - impl Position for Position4D { - fn neighbors(&self) -> Vec { - iproduct!((-1..=1), (-1..=1), (-1..=1), (-1..=1)) - .filter(|t| t != &(0, 0, 0, 0)) - .map(|(x, y, z, w)| *self + Position4D::from((x, y, z, w))) - .collect() - } - } - - impl From<(I, I, I, I)> for Position4D - where I: TryInto - { - fn from((x, y, z, w): (I, I, I, I)) -> Position4D { - Position4D { - x: unwrap_number_result(x), - y: unwrap_number_result(y), - z: unwrap_number_result(z), - w: unwrap_number_result(w), - } - } - } - - impl_op!(-|a: Position4D, b: Position4D| -> Position4D { - Position4D { - x: a.x - b.x, - y: a.y - b.y, - z: a.z - b.z, - w: a.w - b.w, - } - }); - - impl_op!(+|a: Position4D, b: Position4D| -> Position4D { - Position4D { - x: a.x + b.x, - y: a.y + b.y, - z: a.z + b.z, - w: a.w + b.w, - } - }); -} - -// because calling .unwrap() on a TryInto result isn’t possible without trait bounds on the -// associated Error type. -fn unwrap_number_result>(i: I) -> i64 { - match i.try_into() { - Ok(i) => i, - _ => panic!("Bad coordinate"), - } -} - #[cfg(test)] mod tests { use super::*;