Compare commits
3 commits
7896aafcd7
...
daad18c6af
Author | SHA1 | Date | |
---|---|---|---|
daad18c6af | |||
e1862bdcbb | |||
d627f1d00f |
2 changed files with 48 additions and 17 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#![feature(iterator_try_reduce)]
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -7,15 +8,14 @@ use std::{
|
||||||
|
|
||||||
use aoc::vec2::{Direction, Vec2};
|
use aoc::vec2::{Direction, Vec2};
|
||||||
|
|
||||||
fn tail_movement(following: Vec2, tail: Vec2) -> Vec2 {
|
fn knot_movement(previous: Vec2, knot: Vec2) -> Option<Vec2> {
|
||||||
let (dx, dy) = (following - tail).into();
|
let (dx, dy) = (previous - knot).into();
|
||||||
|
|
||||||
let movement = if dx.abs() <= 1 && dy.abs() <= 1 {
|
if dx.abs() <= 1 && dy.abs() <= 1 {
|
||||||
(0, 0)
|
None
|
||||||
} else {
|
} else {
|
||||||
(dx.signum(), dy.signum())
|
Some((dx.signum(), dy.signum()).into())
|
||||||
};
|
}
|
||||||
movement.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simulate_knots(num_knots: usize, instructions: &[(Direction, usize)]) -> usize {
|
fn simulate_knots(num_knots: usize, instructions: &[(Direction, usize)]) -> usize {
|
||||||
|
@ -31,15 +31,12 @@ fn simulate_knots(num_knots: usize, instructions: &[(Direction, usize)]) -> usiz
|
||||||
for _i in 0..*distance {
|
for _i in 0..*distance {
|
||||||
knots[0] += Vec2::from(*direction);
|
knots[0] += Vec2::from(*direction);
|
||||||
|
|
||||||
let tail = knots
|
if let Some(tail) = knots.iter_mut().try_reduce(|previous, knot| {
|
||||||
.iter_mut()
|
*knot += knot_movement(*previous, *knot)?;
|
||||||
.reduce(|following, knot| {
|
Some(knot)
|
||||||
*knot += tail_movement(*following, *knot);
|
}) {
|
||||||
knot
|
tail_positions.insert(tail.copied().unwrap());
|
||||||
})
|
}
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
tail_positions.insert(*tail);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
ops::{Add, AddAssign, Sub, SubAssign},
|
fmt,
|
||||||
|
ops::{Add, AddAssign, Mul, Sub, SubAssign},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,6 +23,17 @@ impl Vec2 {
|
||||||
y: f(self.y),
|
y: f(self.y),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn len(self) -> f64 {
|
||||||
|
(f64::from(self.x).powi(2) + f64::from(self.y).powi(2)).sqrt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Vec2 {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{:?}", (self.x, self.y))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add for Vec2 {
|
impl Add for Vec2 {
|
||||||
|
@ -60,6 +72,28 @@ impl SubAssign for Vec2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mul<i32> for Vec2 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, n: i32) -> Self::Output {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x * n,
|
||||||
|
y: self.y * n,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<Vec2> for i32 {
|
||||||
|
type Output = Vec2;
|
||||||
|
|
||||||
|
fn mul(self, Vec2 { x, y }: Vec2) -> Self::Output {
|
||||||
|
Vec2 {
|
||||||
|
x: x * self,
|
||||||
|
y: y * self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<(i32, i32)> for Vec2 {
|
impl From<(i32, i32)> for Vec2 {
|
||||||
fn from((x, y): (i32, i32)) -> Self {
|
fn from((x, y): (i32, i32)) -> Self {
|
||||||
Self { x, y }
|
Self { x, y }
|
||||||
|
|
Loading…
Reference in a new issue