diff --git a/2022/day_8/rust/src/main.rs b/2022/day_8/rust/src/main.rs index 8f2e462..fcc13d4 100644 --- a/2022/day_8/rust/src/main.rs +++ b/2022/day_8/rust/src/main.rs @@ -9,17 +9,18 @@ fn main() { let data = include_str!("../../input.txt"); let grid = Grid::from(data); - // let grid_peak_ranges = GridPeakRanges::from(&grid); + let grid_peak_ranges = GridPeakRanges::from(&grid); - // let visibility_map = grid.get_visible_items_map(&grid_peak_ranges); + let visibility_map = grid.get_visible_items_map(&grid_peak_ranges); - // let sum = visibility_map - // .iter() - // .map(|row| row.iter().map(|&v| v as u32).sum::()) - // .sum::(); + let sum = visibility_map + .iter() + .map(|row| row.iter().map(|&v| v as u32).sum::()) + .sum::(); let scenic_scores = grid.get_scenic_scores(); let max_scenic_score = scenic_scores.iter().max(); + println!("{:#?}", max_scenic_score); - // println!("{sum}"); + println!("{sum}"); } diff --git a/2022/day_9/input.txt b/2022/day_9/input.txt new file mode 100644 index 0000000..c206ba3 --- /dev/null +++ b/2022/day_9/input.txt @@ -0,0 +1,2000 @@ +R 2 +L 2 +D 2 +U 2 +R 2 +D 2 +L 2 +D 2 +U 2 +L 2 +D 1 +R 1 +D 2 +R 2 +U 1 +R 1 +U 2 +R 1 +L 2 +D 2 +L 1 +U 1 +L 1 +D 1 +R 1 +L 2 +U 2 +R 2 +U 1 +R 2 +L 2 +D 1 +U 1 +D 2 +L 1 +U 1 +L 1 +D 1 +U 1 +D 1 +L 2 +D 2 +U 2 +L 2 +U 1 +R 2 +D 2 +L 1 +R 1 +U 1 +R 1 +L 1 +U 2 +D 1 +U 2 +R 1 +U 1 +R 2 +D 2 +R 2 +U 2 +D 2 +U 2 +L 1 +D 2 +R 1 +L 2 +D 2 +R 2 +L 2 +U 2 +L 2 +R 1 +D 1 +R 1 +L 2 +D 1 +L 2 +R 1 +U 1 +R 1 +U 1 +L 2 +U 1 +D 2 +R 1 +L 1 +D 1 +R 1 +L 2 +R 2 +L 2 +R 1 +L 1 +D 1 +L 1 +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16 +U 13 +R 13 +U 14 +L 16 +U 7 +D 2 +R 11 +U 17 +D 9 +R 12 +U 16 +L 11 +R 3 +U 9 +L 6 +D 2 +U 1 +D 7 +L 1 +U 7 +D 8 +U 11 +L 4 +R 6 +L 2 +U 12 +L 13 +U 5 +L 17 +R 16 +D 10 +U 12 +L 7 +R 4 +U 8 +D 17 +R 13 +L 2 +D 5 +L 1 +R 2 +D 16 +U 9 +D 15 +L 8 +U 14 +R 9 +D 18 +U 16 +L 17 +U 9 +D 11 +U 17 +R 8 +D 17 +U 14 +D 8 +U 8 +R 8 +U 14 +R 5 +L 1 +R 3 +D 5 +U 10 +R 16 +U 18 +D 16 +U 11 +D 18 +U 13 +L 11 +D 2 +R 16 +D 15 +L 9 +D 12 +R 11 +L 8 +U 8 +R 10 +L 2 +D 6 +L 5 +D 9 +L 3 +D 1 +U 18 +D 12 +U 13 +L 6 +R 17 +D 10 +R 9 +L 6 +R 3 +U 1 +R 1 +U 6 +L 15 +U 8 +D 15 +U 14 +R 10 +L 6 +U 1 +L 12 +R 9 +D 13 +U 1 +L 16 +U 16 +L 13 +R 4 +U 3 +D 11 +L 12 +R 11 +U 17 +R 18 +L 4 +R 6 +L 18 +D 10 +L 16 +R 2 +D 4 +R 14 +D 11 +L 12 +R 7 +L 9 +U 12 +D 12 +R 4 +L 1 +D 14 +R 14 +L 3 +U 2 +L 6 +R 7 +D 6 +U 18 +L 17 +U 12 +R 12 +L 5 +D 13 +R 7 +D 8 +L 16 +D 13 +U 4 +D 3 +R 16 +L 9 +U 8 +D 15 +R 8 +L 12 +R 5 +L 1 +U 11 +D 11 +L 10 +U 7 +D 6 +L 15 +D 9 +R 10 +D 6 +U 14 +L 7 +U 19 +R 17 +L 12 +U 1 +L 9 +R 17 +D 12 +U 3 +L 15 +R 16 +D 15 +R 15 +L 1 +U 16 +R 19 +D 9 +L 16 +D 12 +R 8 +L 2 +D 16 +L 17 +U 16 +D 9 +L 9 +U 8 +L 9 +R 16 +L 3 +D 8 +U 7 +D 10 +U 17 +R 2 +D 16 +R 6 +U 9 +R 4 +L 17 +D 10 +U 10 +L 14 +D 9 +R 8 +L 9 +R 18 +U 13 +L 16 +R 2 +U 16 +L 11 +U 4 +L 13 +R 8 +L 19 +U 4 +D 10 +R 10 +U 14 +R 13 +L 17 +U 17 +R 12 +D 18 +R 2 +D 5 +L 12 +U 6 +D 7 +R 9 +L 13 +D 15 +R 9 +U 16 +D 14 +R 8 +L 8 +U 10 +L 8 +D 18 +U 8 +D 9 +L 6 +R 8 +U 4 +D 14 +L 13 +D 4 +R 3 +U 13 +L 2 \ No newline at end of file diff --git a/2022/day_9/rust/Cargo.toml b/2022/day_9/rust/Cargo.toml new file mode 100644 index 0000000..a7f9f62 --- /dev/null +++ b/2022/day_9/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_2022_09" +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/2022/day_9/rust/src/main.rs b/2022/day_9/rust/src/main.rs new file mode 100644 index 0000000..06f6055 --- /dev/null +++ b/2022/day_9/rust/src/main.rs @@ -0,0 +1,14 @@ +mod rope; + +use rope::{parse_series_of_motions, Rope}; + +fn main() { + let input = include_str!("../../input.txt"); + + let mut rope = Rope::new(); + let motions = parse_series_of_motions(input); + + motions.iter().for_each(|motion| rope.apply_motion(motion)); + + assert_eq!(rope.get_tails_visited_positions(), 5710); +} diff --git a/2022/day_9/rust/src/rope.rs b/2022/day_9/rust/src/rope.rs new file mode 100644 index 0000000..d672c61 --- /dev/null +++ b/2022/day_9/rust/src/rope.rs @@ -0,0 +1,136 @@ +use std::{ + cmp::{max, min}, + collections::HashSet, +}; + +#[derive(Debug, Clone, Copy)] +pub enum Motion { + Right, + Left, + Up, + Down, +} + +impl From<&str> for Motion { + fn from(string: &str) -> Self { + match string { + "R" => Motion::Right, + "U" => Motion::Up, + "D" => Motion::Down, + "L" => Motion::Left, + _ => panic!(), + } + } +} + +pub fn parse_series_of_motions(string: &str) -> Vec { + let mut motions: Vec = Vec::new(); + + for line in string.lines() { + let (motion, count) = match line.split_once(" ") { + Some((m, c)) => (Motion::from(m), c.parse::().unwrap()), + _ => panic!(), + }; + + for _ in 0..count { + motions.push(motion); + } + } + + return motions; +} + +#[derive(Debug)] +pub struct Rope { + head: (i32, i32), + tail: (i32, i32), + tails_visited_positions: HashSet<(i32, i32)>, +} + +pub fn get_vectors_delta(v1: (i32, i32), v2: (i32, i32)) -> (i32, i32) { + ( + max(v1.0, v2.0) - min(v1.0, v2.0), + max(v1.1, v2.1) - min(v1.1, v2.1), + ) +} + +impl Rope { + pub fn new() -> Self { + let mut tails_visited_positions = HashSet::new(); + tails_visited_positions.insert((0, 0)); + + Self { + head: (0, 0), + tail: (0, 0), + tails_visited_positions, + } + } + + fn get_rope_state(&self) -> (bool, (i32, i32)) { + let delta = get_vectors_delta(self.head, self.tail); + return (!(delta.0 > 1 || delta.1 > 1), delta); + } + + pub fn move_tail_to_head(&mut self, delta: (i32, i32)) { + if delta.0 >= 1 { + if self.head.0 > self.tail.0 { + self.tail.0 += 1; + } else { + self.tail.0 -= 1; + } + } + + if delta.1 >= 1 { + if self.head.1 > self.tail.1 { + self.tail.1 += 1; + } else { + self.tail.1 -= 1; + } + } + } + + pub fn apply_motion(&mut self, motion: &Motion) { + match *motion { + Motion::Down => self.head.1 -= 1, + Motion::Left => self.head.0 -= 1, + Motion::Right => self.head.0 += 1, + Motion::Up => self.head.1 += 1, + } + + let (is_touch, delta) = self.get_rope_state(); + + if !is_touch { + self.move_tail_to_head(delta); + self.tails_visited_positions.insert(self.tail); + } + } + + pub fn get_tails_visited_positions(&self) -> usize { + self.tails_visited_positions.len() + } +} + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn demo() { + let input = "\ + R 4\n\ + U 4\n\ + L 3\n\ + D 1\n\ + R 4\n\ + D 1\n\ + L 5\n\ + R 2"; + + let mut rope = Rope::new(); + let motions = parse_series_of_motions(input); + + motions.iter().for_each(|motion| rope.apply_motion(motion)); + + assert_eq!(rope.tails_visited_positions.len(), 13); + } +} diff --git a/Cargo.lock b/Cargo.lock index b53ecdb..1f828d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,3 +33,7 @@ version = "0.1.0" [[package]] name = "rust_2022_08" version = "0.1.0" + +[[package]] +name = "rust_2022_09" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 70f5a7a..aee68cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "2022/day_5/rust", "2022/day_6/rust", "2022/day_7/rust", - "2022/day_8/rust" + "2022/day_8/rust", + "2022/day_9/rust" ] exclude = [ "2022" ] \ No newline at end of file