From e76573b78c67a8637c753ad7f266758d4fc9107e Mon Sep 17 00:00:00 2001 From: Sebastian Knackstedt Date: Fri, 9 Dec 2022 15:44:54 +0100 Subject: [PATCH] Add day 9 --- Cargo.toml | 4 + day9.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++++ day9_test.txt | 8 + day9_test_2.txt | 8 + src/day9/main.rs | 116 +++ 5 files changed, 2136 insertions(+) create mode 100644 day9.txt create mode 100644 day9_test.txt create mode 100644 day9_test_2.txt create mode 100644 src/day9/main.rs diff --git a/Cargo.toml b/Cargo.toml index c74297c..57bc582 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,4 +41,8 @@ path = "src/day7/main.rs" name = "day8" path = "src/day8/main.rs" +[[bin]] +name = "day9" +path = "src/day9/main.rs" + [dependencies] diff --git a/day9.txt b/day9.txt new file mode 100644 index 0000000..71ff282 --- /dev/null +++ b/day9.txt @@ -0,0 +1,2000 @@ +L 2 +U 2 +L 1 +D 1 +U 1 +L 2 +R 2 +D 1 +U 2 +D 1 +R 2 +U 1 +L 2 +D 2 +L 2 +R 2 +L 2 +R 1 +L 2 +D 1 +U 1 +L 2 +U 1 +D 1 +R 2 +L 1 +R 1 +L 2 +U 2 +D 2 +U 1 +D 2 +R 1 +D 1 +R 2 +L 2 +D 1 +L 1 +U 2 +R 2 +L 2 +U 2 +D 1 +U 2 +D 2 +R 1 +U 2 +L 2 +D 2 +U 2 +D 2 +L 2 +R 1 +L 2 +D 2 +U 1 +R 2 +U 2 +L 1 +D 2 +R 2 +L 2 +R 1 +L 2 +D 1 +R 2 +L 2 +R 1 +L 2 +D 1 +U 1 +L 1 +D 1 +R 2 +D 1 +L 1 +D 1 +R 1 +D 1 +U 2 +R 1 +L 1 +U 2 +L 2 +D 2 +L 1 +U 1 +D 1 +L 1 +U 1 +D 1 +U 1 +R 2 +L 2 +U 2 +R 2 +L 2 +D 2 +U 1 +L 1 +R 1 +U 2 +R 1 +D 2 +R 1 +L 2 +R 2 +U 2 +D 2 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 3 +L 16 +D 4 +U 13 +D 17 +L 5 +R 14 +U 3 +R 12 +U 5 +D 2 +U 9 +R 11 +D 13 +L 6 +U 3 +R 17 +L 11 +R 17 +L 13 +U 2 +R 17 +U 9 +L 3 +R 16 +D 1 +U 13 +D 3 +R 7 +L 9 +R 16 +D 17 +L 3 +U 3 +R 7 +L 15 +U 3 +R 6 +L 11 +R 11 +D 2 +U 1 +L 4 +D 6 +L 15 +R 9 +D 14 +U 1 +L 1 +U 10 +L 18 +D 4 +R 12 +L 14 +R 11 +D 14 +L 15 +U 17 +L 6 +U 11 +D 14 +U 1 +L 9 +D 8 +U 6 +L 18 +R 2 +D 4 +L 1 +D 13 +L 17 +U 6 +L 18 +U 16 +R 1 +U 8 +R 8 +L 17 +D 13 +U 18 +R 5 +U 6 +R 14 +D 4 +U 16 +L 18 +U 6 +R 18 +L 3 +U 12 +R 2 +D 9 +U 6 +R 11 +L 2 +D 11 +U 11 +D 16 +L 17 +U 18 +D 7 +R 12 +D 2 +L 9 +R 4 +L 3 +R 3 +U 17 +L 15 +R 12 +U 16 +L 12 +U 2 +L 8 +D 3 +L 10 +U 11 +L 2 +D 18 +R 1 +D 2 +R 5 +D 9 +L 15 +D 8 +L 18 +D 7 +L 16 +R 2 +U 17 +L 14 +R 11 +D 2 +U 12 +L 5 +D 6 +R 17 +L 7 +U 10 +R 1 +L 17 +R 12 +U 2 +D 4 +U 13 +D 11 +U 16 +L 4 +R 10 +U 3 +R 14 +U 16 +L 15 +U 10 +L 12 +U 19 +L 19 +R 15 +L 3 +U 6 +R 19 +L 14 +R 4 +L 6 +R 5 +L 17 +U 15 +L 6 +D 11 +U 7 +L 2 +R 8 +U 8 +R 16 +L 4 +U 19 +D 9 +L 15 +D 15 +L 16 +D 15 +L 9 +R 5 +L 10 +U 4 +R 15 +U 1 +D 4 +U 18 +R 18 +L 1 +U 18 +R 8 +L 11 +R 12 +U 3 +R 16 +D 17 +R 2 +L 18 +D 9 +R 5 +U 13 +R 12 +U 5 +R 6 +U 18 +R 5 +D 1 +L 8 +D 6 +L 16 +D 19 +L 16 +R 10 +L 9 +U 17 +D 19 +R 6 +D 15 +R 4 +U 19 +D 16 +R 13 +U 5 +R 10 +U 16 +D 4 +U 19 +R 17 +L 11 +U 4 +L 1 +U 9 +R 16 +L 6 +U 12 +D 9 +L 9 +R 3 +U 15 +R 18 +D 4 +U 19 +R 8 +L 3 +U 5 +D 18 +R 18 +D 17 +L 18 +R 15 +U 12 \ No newline at end of file diff --git a/day9_test.txt b/day9_test.txt new file mode 100644 index 0000000..cbea2b3 --- /dev/null +++ b/day9_test.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 \ No newline at end of file diff --git a/day9_test_2.txt b/day9_test_2.txt new file mode 100644 index 0000000..c1eba0a --- /dev/null +++ b/day9_test_2.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20 \ No newline at end of file diff --git a/src/day9/main.rs b/src/day9/main.rs new file mode 100644 index 0000000..68700e9 --- /dev/null +++ b/src/day9/main.rs @@ -0,0 +1,116 @@ +use std::collections::HashSet; +use base::read_file; + +#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)] +struct Position { + x: i32, + y: i32, +} + + +#[derive(Debug)] +enum Direction { + UP, + DOWN, + LEFT, + RIGHT, +} + +#[derive(Debug)] +struct MoveCommand(Direction, i32); + +impl Position { + fn move_by(&mut self, dir: &Direction) { + match dir { + Direction::UP => self.y += 1, + Direction::DOWN => self.y -= 1, + Direction::LEFT => self.x -= 1, + Direction::RIGHT => self.x += 1 + } + } + + fn move_to(&mut self, other: &Position) { + if self.x > other.x { + self.x -= 1; + } else if self.x < other.x { + self.x += 1; + } + + if self.y > other.y { + self.y -= 1; + } else if self.y < other.y { + self.y += 1; + } + } +} + +struct Map { + rope: [Position; N], + history: HashSet, +} + +impl Default for Map { + fn default() -> Self { + assert!(N > 1, "Length must be larger than one"); + Map { + rope: [Position { x: 0, y: 0 }; N], + history: Default::default(), + } + } +} + +impl Map { + fn move_head(&mut self, cmd: &MoveCommand) { + for _ in 0..cmd.1 { + self.rope[0].move_by(&cmd.0); + + for rope_element in 1..N { + if !self.is_in_reach(rope_element) { + let element_before = self.rope[rope_element - 1]; + self.rope[rope_element].move_to(&element_before); + if rope_element == N - 1 { + self.history.insert(self.rope[rope_element]); + } + } + } + } + } + + fn is_in_reach(&self, which: usize) -> bool { + (self.rope[which - 1].x - self.rope[which].x).abs() <= 1 && (self.rope[which].y - self.rope[which - 1].y).abs() <= 1 + } +} + +fn main() { + let mut commands = vec![]; + + for line in read_file("day9.txt") { + let splits = line.split_whitespace().collect::>(); + let count = splits[1].parse::().unwrap(); + commands.push(match splits[0] { + "U" => MoveCommand(Direction::UP, count), + "L" => MoveCommand(Direction::LEFT, count), + "D" => MoveCommand(Direction::DOWN, count), + "R" => MoveCommand(Direction::RIGHT, count), + _ => { + panic!("Unknown option") + } + }); + } + + let mut task_1 = Map::<2>::default(); + + for command in &commands { + task_1.move_head(command); + } + + println!("Tail was at {} unique fields", task_1.history.len() + 1); + + let mut task_2 = Map::<10>::default(); + + for command in &commands { + task_2.move_head(command); + } + + println!("Tail was at {} unique fields", task_2.history.len() + 1); +} \ No newline at end of file