adventofcode2022/day09/common.rs

144 lines
4.5 KiB
Rust
Raw Normal View History

2022-12-10 17:05:03 +01:00
// 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/>.
use std::io;
use std::str::FromStr;
#[derive(Clone, Copy)]
enum Direction {
Up,
Right,
Down,
Left,
}
#[derive(Debug, Clone, Copy)]
2022-12-10 17:56:01 +01:00
pub struct RopeState<const LEN: usize> {
knots: [(isize, isize); LEN],
2022-12-10 17:05:03 +01:00
}
2022-12-10 17:56:01 +01:00
pub struct RopeStates<const LEN: usize> {
2022-12-10 17:05:03 +01:00
direction: Direction,
count: isize,
2022-12-10 17:56:01 +01:00
state: RopeState<LEN>,
2022-12-10 17:05:03 +01:00
}
impl FromStr for Direction {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"U" => Ok(Self::Up),
"R" => Ok(Self::Right),
"D" => Ok(Self::Down),
"L" => Ok(Self::Left),
_ => Err("cannot parse direction"),
}
}
}
2022-12-10 17:56:01 +01:00
impl<const LEN: usize> RopeState<LEN> {
2022-12-10 17:05:03 +01:00
pub fn new() -> Self {
2022-12-10 17:56:01 +01:00
Self {
knots: [(0, 0); LEN],
2022-12-10 17:05:03 +01:00
}
}
pub fn head_pos(&self) -> (isize, isize) {
2022-12-10 17:56:01 +01:00
self.knots[0]
2022-12-10 17:05:03 +01:00
}
pub fn tail_pos(&self) -> (isize, isize) {
2022-12-10 17:56:01 +01:00
self.knots[LEN - 1]
2022-12-10 17:05:03 +01:00
}
2022-12-10 17:56:01 +01:00
fn adjust_knot(&mut self, index: usize) {
let x_diff = self.knots[index - 1].0.abs_diff(self.knots[index].0);
let y_diff = self.knots[index - 1].1.abs_diff(self.knots[index].1);
2022-12-10 17:05:03 +01:00
let (move_x, move_y) = match (x_diff, y_diff) {
(2, 0) => (true, false),
(2, 1) => (true, true),
(0, 2) => (false, true),
(1, 2) => (true, true),
2022-12-10 17:56:01 +01:00
(2, 2) => (true, true),
2022-12-10 17:05:03 +01:00
_ => (false, false),
};
if move_x {
2022-12-10 17:56:01 +01:00
match (self.knots[index - 1].0, self.knots[index].0) {
(h, t) if { h > t } => self.knots[index].0 += 1,
(h, t) if { h < t } => self.knots[index].0 -= 1,
2022-12-10 17:05:03 +01:00
_ => (),
}
}
if move_y {
2022-12-10 17:56:01 +01:00
match (self.knots[index - 1].1, self.knots[index].1) {
(h, t) if { h > t } => self.knots[index].1 += 1,
(h, t) if { h < t } => self.knots[index].1 -= 1,
2022-12-10 17:05:03 +01:00
_ => (),
}
}
}
2022-12-10 17:56:01 +01:00
fn move_head(&mut self, direction: Direction) -> Result<RopeState<LEN>, &'static str> {
2022-12-10 17:05:03 +01:00
const ERROR: &'static str = "invalid move";
match direction {
2022-12-10 17:56:01 +01:00
Direction::Up => self.knots[0].1 = self.knots[0].1.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.knots[0].1 = self.knots[0].1.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);
2022-12-10 17:05:03 +01:00
}
Ok(*self)
}
}
2022-12-10 17:56:01 +01:00
impl<const LEN: usize> Iterator for RopeStates<LEN> {
type Item = Result<RopeState<LEN>, &'static str>;
2022-12-10 17:05:03 +01:00
fn next(&mut self) -> Option<Self::Item> {
if self.count == 0 {
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(_) if { line == "" || line == "\n" } => return None,
Ok(_) => {
if let Some((left, right)) = line.trim().split_once(' ') {
if let (Ok(direction), Ok(count)) = (left.parse(), right.parse()) {
self.direction = direction;
self.count = count;
} else {
return Some(Err("input file contains invalid line"));
}
} else {
return Some(Err("input file contains invalid line"));
}
}
Err(_) => assert!(false),
}
}
self.count -= 1;
Some(self.state.move_head(self.direction))
}
}
2022-12-10 17:56:01 +01:00
pub fn rope_states<const LEN: usize>() -> RopeStates<LEN> {
2022-12-10 17:05:03 +01:00
RopeStates {
direction: Direction::Up,
count: 0,
state: RopeState::new(),
}
}