add day 9, part 2
This commit is contained in:
parent
ca71251954
commit
e7d5791adb
|
@ -25,15 +25,14 @@ enum Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct RopeState {
|
pub struct RopeState<const LEN: usize> {
|
||||||
head_pos: (isize, isize),
|
knots: [(isize, isize); LEN],
|
||||||
tail_pos: (isize, isize),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RopeStates {
|
pub struct RopeStates<const LEN: usize> {
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
count: isize,
|
count: isize,
|
||||||
state: RopeState,
|
state: RopeState<LEN>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Direction {
|
impl FromStr for Direction {
|
||||||
|
@ -50,63 +49,65 @@ impl FromStr for Direction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RopeState {
|
impl<const LEN: usize> RopeState<LEN> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
RopeState {
|
Self {
|
||||||
head_pos: (0, 0),
|
knots: [(0, 0); LEN],
|
||||||
tail_pos: (0, 0),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn head_pos(&self) -> (isize, isize) {
|
pub fn head_pos(&self) -> (isize, isize) {
|
||||||
self.head_pos
|
self.knots[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tail_pos(&self) -> (isize, isize) {
|
pub fn tail_pos(&self) -> (isize, isize) {
|
||||||
self.tail_pos
|
self.knots[LEN - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn adjust_tail(&mut self) {
|
fn adjust_knot(&mut self, index: usize) {
|
||||||
let x_diff = self.head_pos.0.abs_diff(self.tail_pos.0);
|
let x_diff = self.knots[index - 1].0.abs_diff(self.knots[index].0);
|
||||||
let y_diff = self.head_pos.1.abs_diff(self.tail_pos.1);
|
let y_diff = self.knots[index - 1].1.abs_diff(self.knots[index].1);
|
||||||
let (move_x, move_y) = match (x_diff, y_diff) {
|
let (move_x, move_y) = match (x_diff, y_diff) {
|
||||||
(2, 0) => (true, false),
|
(2, 0) => (true, false),
|
||||||
(2, 1) => (true, true),
|
(2, 1) => (true, true),
|
||||||
(0, 2) => (false, true),
|
(0, 2) => (false, true),
|
||||||
(1, 2) => (true, true),
|
(1, 2) => (true, true),
|
||||||
|
(2, 2) => (true, true),
|
||||||
_ => (false, false),
|
_ => (false, false),
|
||||||
};
|
};
|
||||||
if move_x {
|
if move_x {
|
||||||
match (self.head_pos.0, self.tail_pos.0) {
|
match (self.knots[index - 1].0, self.knots[index].0) {
|
||||||
(h, t) if { h > t } => self.tail_pos.0 += 1,
|
(h, t) if { h > t } => self.knots[index].0 += 1,
|
||||||
(h, t) if { h < t } => self.tail_pos.0 -= 1,
|
(h, t) if { h < t } => self.knots[index].0 -= 1,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if move_y {
|
if move_y {
|
||||||
match (self.head_pos.1, self.tail_pos.1) {
|
match (self.knots[index - 1].1, self.knots[index].1) {
|
||||||
(h, t) if { h > t } => self.tail_pos.1 += 1,
|
(h, t) if { h > t } => self.knots[index].1 += 1,
|
||||||
(h, t) if { h < t } => self.tail_pos.1 -= 1,
|
(h, t) if { h < t } => self.knots[index].1 -= 1,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_head(&mut self, direction: Direction) -> Result<RopeState, &'static str> {
|
fn move_head(&mut self, direction: Direction) -> Result<RopeState<LEN>, &'static str> {
|
||||||
const ERROR: &'static str = "invalid move";
|
const ERROR: &'static str = "invalid move";
|
||||||
match direction {
|
match direction {
|
||||||
Direction::Up => self.head_pos.1 = self.head_pos.1.checked_add(1).ok_or(ERROR)?,
|
Direction::Up => self.knots[0].1 = self.knots[0].1.checked_add(1).ok_or(ERROR)?,
|
||||||
Direction::Right => self.head_pos.0 = self.head_pos.0.checked_add(1).ok_or(ERROR)?,
|
Direction::Right => self.knots[0].0 = self.knots[0].0.checked_add(1).ok_or(ERROR)?,
|
||||||
Direction::Down => self.head_pos.1 = self.head_pos.1.checked_sub(1).ok_or(ERROR)?,
|
Direction::Down => self.knots[0].1 = self.knots[0].1.checked_sub(1).ok_or(ERROR)?,
|
||||||
Direction::Left => self.head_pos.0 = self.head_pos.0.checked_sub(1).ok_or(ERROR)?,
|
Direction::Left => self.knots[0].0 = self.knots[0].0.checked_sub(1).ok_or(ERROR)?,
|
||||||
|
}
|
||||||
|
for i in 1..LEN {
|
||||||
|
self.adjust_knot(i);
|
||||||
}
|
}
|
||||||
self.adjust_tail();
|
|
||||||
Ok(*self)
|
Ok(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for RopeStates {
|
impl<const LEN: usize> Iterator for RopeStates<LEN> {
|
||||||
type Item = Result<RopeState, &'static str>;
|
type Item = Result<RopeState<LEN>, &'static str>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if self.count == 0 {
|
if self.count == 0 {
|
||||||
|
@ -133,7 +134,7 @@ impl Iterator for RopeStates {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rope_states() -> RopeStates {
|
pub fn rope_states<const LEN: usize>() -> RopeStates<LEN> {
|
||||||
RopeStates {
|
RopeStates {
|
||||||
direction: Direction::Up,
|
direction: Direction::Up,
|
||||||
count: 0,
|
count: 0,
|
||||||
|
|
|
@ -20,8 +20,9 @@ pub mod common;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
fn main() -> Result<(), &'static str> {
|
fn main() -> Result<(), &'static str> {
|
||||||
let tail_positions: Result<HashSet<(isize, isize)>, _> =
|
let tail_positions: Result<HashSet<(isize, isize)>, _> = common::rope_states::<2>()
|
||||||
common::rope_states().map(|s| Ok(s?.tail_pos())).collect();
|
.map(|s| Ok(s?.tail_pos()))
|
||||||
|
.collect();
|
||||||
println!("{}", tail_positions?.len());
|
println!("{}", tail_positions?.len());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
// Copyright 2022 Christian Ulrich
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
// usage: ./part2 < input
|
||||||
|
|
||||||
|
pub mod common;
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
fn main() -> Result<(), &'static str> {
|
||||||
|
let tail_positions: Result<HashSet<(isize, isize)>, _> = common::rope_states::<10>()
|
||||||
|
.map(|s| Ok(s?.tail_pos()))
|
||||||
|
.collect();
|
||||||
|
println!("{}", tail_positions?.len());
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue