diff --git a/Cargo.lock b/Cargo.lock index 2e31d83..d8e5af5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,10 @@ dependencies = [ name = "day08" version = "0.1.0" +[[package]] +name = "day09" +version = "0.1.0" + [[package]] name = "memchr" version = "2.6.4" diff --git a/days/day09/Cargo.toml b/days/day09/Cargo.toml new file mode 100644 index 0000000..1aa04f3 --- /dev/null +++ b/days/day09/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "day09" +authors.workspace = true +description.workspace = true +version.workspace = true diff --git a/days/day09/src/input.txt b/days/day09/src/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/days/day09/src/main.rs b/days/day09/src/main.rs new file mode 100644 index 0000000..90fb2cd --- /dev/null +++ b/days/day09/src/main.rs @@ -0,0 +1,17 @@ +//TODO: +#![allow(dead_code)] +mod part1; +mod part2; +mod parse; +mod rope; + +fn main() { + let input = include_str!("./input.txt"); + let structured_input = parse::parse(input); + + println!("Part One"); + println!("Result: {}", part1::part1()); + + println!("Part Two"); + println!("Result: {}", part2::part2()); +} diff --git a/days/day09/src/parse.rs b/days/day09/src/parse.rs new file mode 100644 index 0000000..0e4ac55 --- /dev/null +++ b/days/day09/src/parse.rs @@ -0,0 +1,65 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Direction { + Up, + Right, + Down, + Left, +} + +pub fn parse(input: &str) -> Vec { + let mut ret = Vec::new(); + for line in input.lines() { + let mut chars = line.chars(); + let dir = match chars.next().unwrap() { + 'U' => Direction::Up, + 'R' => Direction::Right, + 'D' => Direction::Down, + 'L' => Direction::Left, + _ => panic!("invalid direction char"), + }; + for _ in 0..chars.nth(1).unwrap().to_digit(10).unwrap() { + ret.push(dir) + } + } + ret +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse() { + let input = + concat!("R 4\n", "U 4\n", "L 3\n", "D 1\n", "R 4\n", "D 1\n", "L 5\n", "R 2\n",); + assert_eq!( + parse(input), + vec![ + Direction::Right, + Direction::Right, + Direction::Right, + Direction::Right, + Direction::Up, + Direction::Up, + Direction::Up, + Direction::Up, + Direction::Left, + Direction::Left, + Direction::Left, + Direction::Down, + Direction::Right, + Direction::Right, + Direction::Right, + Direction::Right, + Direction::Down, + Direction::Left, + Direction::Left, + Direction::Left, + Direction::Left, + Direction::Left, + Direction::Right, + Direction::Right, + ] + ); + } +} diff --git a/days/day09/src/part1.rs b/days/day09/src/part1.rs new file mode 100644 index 0000000..c268965 --- /dev/null +++ b/days/day09/src/part1.rs @@ -0,0 +1,16 @@ +//TODO: +#![allow(unused)] + +pub fn part1() -> usize { + unimplemented!() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part1() { + assert_eq!(0, 0); + } +} diff --git a/days/day09/src/part2.rs b/days/day09/src/part2.rs new file mode 100644 index 0000000..b45d3f6 --- /dev/null +++ b/days/day09/src/part2.rs @@ -0,0 +1,16 @@ +//TODO: +#![allow(unused)] + +pub fn part2() -> usize { + unimplemented!() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part2() { + assert_eq!(0, 0); + } +} diff --git a/days/day09/src/rope.rs b/days/day09/src/rope.rs new file mode 100644 index 0000000..6c41300 --- /dev/null +++ b/days/day09/src/rope.rs @@ -0,0 +1,66 @@ +use crate::parse::Direction; +#[derive(Debug, Clone, Copy)] +struct Point { + x: isize, + y: isize, +} + +impl std::ops::Add for Point { + type Output = Point; + + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} + +impl std::ops::Sub for Point { + type Output = Point; + + fn sub(self, rhs: Self) -> Self::Output { + Self { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +// L is the length of the rope in segments. +#[derive(Debug)] +struct Rope { + segments: [Point; L], +} + +impl Rope { + pub fn new()->Rope{ + Rope { segments: () } + } + + pub fn update_rope(&mut self, direction: Direction) { + todo!() + } + + pub fn get_tail_pos(&self)->&Point{ + &self.segments[self.segments.len()-1] + } + + fn update_single_segment_pair(head: Point, tail: Point) -> Point { + let delta = head - tail; + if delta.x.abs() > 2 || delta.y.abs() > 2 { + panic!("invalid delta ({}, {})", delta.y, delta.x) + } + match (delta.x, delta.y) { + (0, 2) => Point { x: 0, y: 1 }, + (2, 0) => Point { x: 1, y: 0 }, + (0, -2) => Point { x: 0, y: -1 }, + (-2, 0) => Point { x: -1, y: 0 }, + (1, 2) | (2, 2) | (2, 1) => Point { x: 1, y: 1 }, + (2, -1) | (2, -2) | (1, -2) => Point { x: 1, y: -1 }, + (-1, -2) | (-2, -2) | (-2, -1) => Point { x: -1, y: -1 }, + (-2, 1) | (-2, 2) | (-1, 2) => Point { x: -1, y: 1 }, + _ => Point { x: 0, y: 0 }, + } + } +}