add day 9, part 1

This commit is contained in:
Christian Ulrich 2022-12-10 17:05:03 +01:00
parent 5a22cb51ed
commit ca71251954
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
4 changed files with 2178 additions and 0 deletions

142
day09/common.rs Normal file
View File

@ -0,0 +1,142 @@
// 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)]
pub struct RopeState {
head_pos: (isize, isize),
tail_pos: (isize, isize),
}
pub struct RopeStates {
direction: Direction,
count: isize,
state: RopeState,
}
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"),
}
}
}
impl RopeState {
pub fn new() -> Self {
RopeState {
head_pos: (0, 0),
tail_pos: (0, 0),
}
}
pub fn head_pos(&self) -> (isize, isize) {
self.head_pos
}
pub fn tail_pos(&self) -> (isize, isize) {
self.tail_pos
}
fn adjust_tail(&mut self) {
let x_diff = self.head_pos.0.abs_diff(self.tail_pos.0);
let y_diff = self.head_pos.1.abs_diff(self.tail_pos.1);
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),
_ => (false, false),
};
if move_x {
match (self.head_pos.0, self.tail_pos.0) {
(h, t) if { h > t } => self.tail_pos.0 += 1,
(h, t) if { h < t } => self.tail_pos.0 -= 1,
_ => (),
}
}
if move_y {
match (self.head_pos.1, self.tail_pos.1) {
(h, t) if { h > t } => self.tail_pos.1 += 1,
(h, t) if { h < t } => self.tail_pos.1 -= 1,
_ => (),
}
}
}
fn move_head(&mut self, direction: Direction) -> Result<RopeState, &'static str> {
const ERROR: &'static str = "invalid move";
match direction {
Direction::Up => self.head_pos.1 = self.head_pos.1.checked_add(1).ok_or(ERROR)?,
Direction::Right => self.head_pos.0 = self.head_pos.0.checked_add(1).ok_or(ERROR)?,
Direction::Down => self.head_pos.1 = self.head_pos.1.checked_sub(1).ok_or(ERROR)?,
Direction::Left => self.head_pos.0 = self.head_pos.0.checked_sub(1).ok_or(ERROR)?,
}
self.adjust_tail();
Ok(*self)
}
}
impl Iterator for RopeStates {
type Item = Result<RopeState, &'static str>;
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))
}
}
pub fn rope_states() -> RopeStates {
RopeStates {
direction: Direction::Up,
count: 0,
state: RopeState::new(),
}
}

2001
day09/input Normal file

File diff suppressed because it is too large Load Diff

27
day09/part1.rs Normal file
View File

@ -0,0 +1,27 @@
// 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: ./part1 < input
pub mod common;
use std::collections::HashSet;
fn main() -> Result<(), &'static str> {
let tail_positions: Result<HashSet<(isize, isize)>, _> =
common::rope_states().map(|s| Ok(s?.tail_pos())).collect();
println!("{}", tail_positions?.len());
Ok(())
}

8
day09/testinput Normal file
View File

@ -0,0 +1,8 @@
R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2