From a93031e64315723493f3cdf98f8e20cb2fa3bd9f Mon Sep 17 00:00:00 2001 From: Suguivy Date: Fri, 9 Dec 2022 14:48:55 +0100 Subject: [PATCH] day 9 :3 --- Cargo.lock | 4 + Cargo.toml | 2 +- day09/Cargo.toml | 8 + day09/input | 2000 +++++++++++++++++++++++++++++++++++++++++++++ day09/src/main.rs | 91 +++ 5 files changed, 2104 insertions(+), 1 deletion(-) create mode 100644 day09/Cargo.toml create mode 100644 day09/input create mode 100644 day09/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index f1a0d60..8d426ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,10 @@ version = "0.1.0" name = "day08" version = "0.1.0" +[[package]] +name = "day09" +version = "0.1.0" + [[package]] name = "tiesto" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 32499e6..43ddd1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ members = [ "day06", "day07", "day08", -# "day09", + "day09", # "day10", # "day11", # "day12", diff --git a/day09/Cargo.toml b/day09/Cargo.toml new file mode 100644 index 0000000..15d9cf9 --- /dev/null +++ b/day09/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day09" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day09/input b/day09/input new file mode 100644 index 0000000..b429d1b --- /dev/null +++ b/day09/input @@ -0,0 +1,2000 @@ +R 1 +U 1 +D 1 +U 1 +R 2 +D 2 +R 2 +D 2 +R 1 +U 1 +L 1 +U 2 +D 2 +R 1 +D 1 +R 2 +U 2 +R 2 +L 2 +D 2 +L 2 +U 2 +D 2 +L 2 +R 2 +D 1 +U 2 +D 2 +U 1 +L 2 +D 2 +R 1 +U 2 +D 2 +L 1 +D 2 +L 2 +R 2 +U 2 +D 2 +U 1 +R 1 +U 1 +D 1 +R 2 +L 2 +D 1 +U 1 +R 1 +L 1 +U 1 +D 1 +L 1 +U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 +R 6 +D 4 +R 6 +D 5 +U 8 +D 10 +R 13 +D 6 +U 3 +L 5 +U 3 +R 11 +D 11 +U 8 +L 5 +U 11 +L 7 +R 16 +L 11 +D 3 +L 16 +D 14 +R 1 +U 10 +L 7 +U 4 +D 11 +L 9 +R 3 +D 1 +L 7 +R 17 +D 17 +R 11 +U 4 +R 15 +L 10 +R 13 +U 17 +D 16 +L 16 +U 14 +D 11 +U 10 +D 12 +L 11 +R 5 +D 17 +U 1 +R 5 +L 14 +D 14 +R 5 +L 6 +R 11 +U 13 +R 10 +U 15 +L 2 +R 8 +D 3 +R 11 +L 8 +R 11 +U 5 +L 12 +U 2 +L 16 +D 5 +L 17 +R 10 +D 15 +L 16 +U 1 +D 10 +R 13 +L 6 +D 5 +R 13 +U 2 +D 16 +L 9 +U 11 +D 13 +U 16 +L 6 +R 15 +D 10 +U 17 +L 15 +D 7 +R 6 +D 15 +R 11 +L 14 +U 12 +D 18 +R 6 +U 16 +L 1 +U 10 +R 15 +U 3 +L 12 +D 9 +L 11 +D 18 +R 11 +L 12 +U 5 +L 13 +D 2 +R 11 +D 10 +L 15 +R 5 +D 17 +R 6 +L 16 +R 11 +D 1 +R 5 +U 13 +R 11 +D 13 +L 12 +U 10 +D 9 +R 1 +D 15 +R 1 +D 17 +R 10 +U 11 +R 14 +L 17 +R 2 +L 1 +D 12 +R 6 +U 18 +R 3 +L 8 +R 11 +L 15 +U 8 +L 7 +D 2 +L 9 +D 9 +L 11 +U 5 +D 8 +U 16 +R 13 +L 7 +D 6 +U 16 +L 9 +U 3 +R 4 +L 12 +U 18 +D 7 +L 10 +R 16 +D 1 +R 4 +U 4 +R 13 +D 6 +L 5 +R 3 +D 10 +U 4 +L 17 +D 9 +R 8 +L 1 +D 8 +L 16 +D 3 +U 3 +R 1 +U 9 +R 13 +D 3 +L 4 +R 6 +D 10 +U 16 +L 9 +U 5 +L 14 +U 8 +D 17 +L 18 +D 5 +U 10 +R 14 +L 8 +U 6 +R 5 +U 19 +L 9 +U 2 +D 7 +U 14 +R 17 +L 16 +D 15 +U 11 +D 4 +L 16 +U 14 +D 6 +L 3 +R 18 +L 17 +D 6 +L 10 +U 7 +D 11 +L 15 +U 12 +L 6 +D 6 +L 14 +U 17 +L 7 +D 2 +L 7 +U 12 +L 17 +R 16 +U 19 +L 12 +U 10 +L 6 +U 3 +D 16 +L 5 +D 15 +L 7 +R 6 +D 2 +R 19 +L 4 +D 18 +R 19 +D 9 +R 11 +L 17 +R 1 +U 15 +R 3 +D 18 +L 13 +U 7 +D 7 +R 15 +U 4 +D 16 +R 6 +U 4 +R 10 +U 10 +R 1 +D 19 +R 13 +U 7 +L 13 +D 15 +U 2 +L 18 +D 5 +U 5 +D 10 +L 8 +D 13 +U 8 +L 15 +D 12 +U 11 +D 17 +L 14 +R 4 +D 19 +L 6 +D 5 +L 13 +D 16 +U 1 +R 14 +U 15 +L 18 +D 7 +L 14 +D 11 +L 7 +D 18 +U 16 +L 1 +R 18 +L 1 +U 19 +R 3 +D 1 diff --git a/day09/src/main.rs b/day09/src/main.rs new file mode 100644 index 0000000..ab82cfc --- /dev/null +++ b/day09/src/main.rs @@ -0,0 +1,91 @@ +use std::fs; +use std::iter; +use std::str::FromStr; +use std::collections::HashSet; + +fn main() { + const FILE_PATH: &str = "input"; + println!("Hi this is the nineth day of AOC2022, first we will read the file {}", FILE_PATH); + let contents = fs::read_to_string(FILE_PATH) + .expect("Should have been able to read the file"); + + println!("The ammount of visited positions are: {}", get_positions(&contents, 1).len()); + println!("The ammount of visited positions are: {}", get_positions(&contents, 9).len()); +} + +fn get_positions(input: &str, num_knots: usize) -> HashSet<(i32, i32)> { + let moves: Vec<_> = input.lines().map(|l| { + let l: Vec<_> = l.split(" ").collect(); + let dir = match &l[0] { + &"U" => (0, 1), + &"D" => (0, -1), + &"L" => (-1, 0), + &"R" => (1, 0), + &_ => unreachable!() + }; + iter::repeat(dir).take(usize::from_str(l[1]).expect("NaN")) + }).flatten().collect(); + let mut head = (0,0); + let mut knots = vec![(0,0); num_knots]; + let mut visited = HashSet::new(); + + for m in moves { + head = (head.0+m.0, head.1+m.1); + knots[0] = move_tail(head, knots[0]); + for i in 1..num_knots { + knots[i] = move_tail(knots[i-1], knots[i]); + } + visited.insert(knots[num_knots-1]); + } + + visited +} + +fn move_tail(head: (i32, i32), tail: (i32, i32)) -> (i32, i32) { + let dx = head.0 - tail.0; + let dy = head.1 - tail.1; + + if dx.abs() <= 1 && dy.abs() <= 1 {return tail;} + + match (dx, dy) { + (2,2) => (tail.0+1, tail.1+1), + (-2,2) => (tail.0-1, tail.1+1), + (2,-2) => (tail.0+1, tail.1-1), + (-2,-2) => (tail.0-1, tail.1-1), + (2,y) => (tail.0+1, tail.1+y), + (-2,y) => (tail.0-1, tail.1+y), + (x,2) => (tail.0+x, tail.1+1), + (x,-2) => (tail.0+x, tail.1-1), + _ => unreachable!() + } + +} + +#[cfg(test)] +mod test { + const INPUT: &str = r#"R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2"#; + const INPUT2: &str = r#"R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20"#; + use super::*; + #[test] + fn rope1() { + assert_eq!(get_positions(INPUT, 1).len(), 13); + } + #[test] + fn rope2() { + assert_eq!(get_positions(INPUT2, 9).len(), 36); + } +} \ No newline at end of file