From 0c1d65edfc3840e2c57f312300b9120c775c20fe Mon Sep 17 00:00:00 2001 From: Burnus Date: Mon, 3 Apr 2023 17:13:24 +0200 Subject: [PATCH] Added Solution for 2020 day 03 --- 2020/day03_toboggan_trajectory/Cargo.toml | 8 + 2020/day03_toboggan_trajectory/challenge.txt | 66 ++++ 2020/day03_toboggan_trajectory/src/lib.rs | 68 ++++ .../tests/challenge_input | 323 ++++++++++++++++++ .../tests/sample_input | 11 + 5 files changed, 476 insertions(+) create mode 100644 2020/day03_toboggan_trajectory/Cargo.toml create mode 100644 2020/day03_toboggan_trajectory/challenge.txt create mode 100644 2020/day03_toboggan_trajectory/src/lib.rs create mode 100644 2020/day03_toboggan_trajectory/tests/challenge_input create mode 100644 2020/day03_toboggan_trajectory/tests/sample_input diff --git a/2020/day03_toboggan_trajectory/Cargo.toml b/2020/day03_toboggan_trajectory/Cargo.toml new file mode 100644 index 0000000..e12b69a --- /dev/null +++ b/2020/day03_toboggan_trajectory/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03_toboggan_trajectory" +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/2020/day03_toboggan_trajectory/challenge.txt b/2020/day03_toboggan_trajectory/challenge.txt new file mode 100644 index 0000000..a47fa59 --- /dev/null +++ b/2020/day03_toboggan_trajectory/challenge.txt @@ -0,0 +1,66 @@ +With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan might be easy, it's certainly not safe: there's very minimal steering and the area is covered in trees. You'll need to see which angles will take you near the fewest trees. + +Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (`.`) and trees (`#`) you can see. For example: + +``` +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# + +``` + +These aren't the only trees, though; due to something you read about once involving arboreal genetics and biome stability, the same pattern repeats to the right many times: + +``` +..##.........##.........##.........##.........##.........##....... ---> +#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.##.......#.##.......#.##.......#.##.......#.##..... ---> +.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........#.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...##....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +``` + +You start on the open square (`.`) in the top-left corner and need to reach the bottom (below the bottom-most row on your map). + +The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by *counting all the trees* you would encounter for the slope *right 3, down 1*: + +From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map. + +The locations you'd check in the above example are marked here with `*O*` where there was an open square and `*X*` where there was a tree: + +``` +..##.........##.........##.........##.........##.........##....... ---> +#..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##..... ---> +.#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........X.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...#X....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +``` + +In this example, traversing the map using this slope would cause you to encounter `*7*` trees. + +Starting at the top-left corner of your map and following a slope of right 3 and down 1, *how many trees would you encounter?* + +To begin, [get your puzzle input](3/input). + +Answer: \ No newline at end of file diff --git a/2020/day03_toboggan_trajectory/src/lib.rs b/2020/day03_toboggan_trajectory/src/lib.rs new file mode 100644 index 0000000..974566b --- /dev/null +++ b/2020/day03_toboggan_trajectory/src/lib.rs @@ -0,0 +1,68 @@ +use core::fmt::Display; + +#[derive(Debug, PartialEq, Eq)] +pub enum ParseError { + InvalidChar(char) +} + +impl Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::InvalidChar(c) => write!(f, "Invalid Character encountered: {c}"), + } + } +} + +struct Grid { + trees: Vec>, +} + +impl TryFrom<&str> for Grid { + type Error = ParseError; + + fn try_from(value: &str) -> Result { + Ok(Self { + trees: value.lines().map(|line| line.chars().map(|c| match c { + '.' => Ok(false), + '#' => Ok(true), + _ => Err(ParseError::InvalidChar(c)), + }).collect::, _>>()).collect::, _>>()?, + }) + } +} + +impl Grid { + fn trees_hit_by_going(&self, (right, down): (usize, usize)) -> usize { + self.trees.iter().enumerate().step_by(down).filter(|(idx, row)| row[(right*idx/down)%row.len()]).count() + } +} + +pub fn run(input: &str) -> Result<(usize, usize), ParseError> { + let grid = Grid::try_from(input)?; + let first = grid.trees_hit_by_going((3, 1)); + let slopes = [(1, 1), (5, 1), (7, 1), (1, 2)]; + let second = slopes.iter().map(|slope| grid.trees_hit_by_going(*slope)).product::() * first; + Ok((first, second)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::read_to_string; + + fn read_file(name: &str) -> String { + read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string() + } + + #[test] + fn test_sample() { + let sample_input = read_file("tests/sample_input"); + assert_eq!(run(&sample_input), Ok((7, 336))); + } + + #[test] + fn test_challenge() { + let challenge_input = read_file("tests/challenge_input"); + assert_eq!(run(&challenge_input), Ok((272, 3898725600))); + } +} diff --git a/2020/day03_toboggan_trajectory/tests/challenge_input b/2020/day03_toboggan_trajectory/tests/challenge_input new file mode 100644 index 0000000..885e360 --- /dev/null +++ b/2020/day03_toboggan_trajectory/tests/challenge_input @@ -0,0 +1,323 @@ +.#...#.......#...#...#.#.#..... +####.....#.#..#...#...........# +.....#...........#......#....#. +......#..#......#.#..#...##.#.# +............#......#........... +...........#.#.#....#.......##. +....#.......#..............#... +........##...#.#.....##...##.#. +.#.#.....##................##.. +.##................##..#...##.. +....#...###...##.........#....# +.##......#.........#........... +...#.#.#....#....#...#...##...# +..#....##...#..#.#..#.....#.#.. +.......#...#..#..#.....#...#..# +.....#......#.......#.....#.#.. +....#..#...#..#####....##...... +.#...........#......#....#....# +#......#.###.....#....#....#... +....#..#.#.#..#...........##... +..#..#..#.#...#......#....#.##. +.##....#......#...#.#..#....... +..###.#...#.........#.#.#...#.# +#....###.........#...#...#...#. +...##.#............#...##...... +...#.........#............#.... +......##...#...##..#........... +........##..#.#.####...#.....#. +.##.........#......#..#..#...#. +..........#...#..........#..... +#..........#........#..#..#.#.. +..#....#.#.#.#.#..#.##......... +##.#.#.##.....#..#......###.... +##....#...#.....#.............. +.#..#...#...#....###......#.... +#....#......#.#..#.#........### +.#....#..#...###....#...##..... +.#....#.....#.....#..##..#..... +#....#.##...#...#..#.##.##.#... +.#.#.#.##...#####.............# +......##..#.....##..#...####... +#.##..#.#....#..##.......###..# +..#.......##....#........#.##.. +#.....#......#.....#....#..#... +.......##...#.....##.......#..# +.......#...#.#.#.........#####. +#.......#.##..##........##..... +##..#...#........##....#....... +.......#...##......##...##.##.. +......#..##..#.#...#...#....##. +....#.#..#.....#.##.#.....#.#.. +#..#.#.#........#...#.......##. +##...........#..#........#..... +....##....#....#.#.......#..... +....##.#.#.....#.#.....#.....#. +..........#.#..##..#..#.......# +#....#.......##...#...#.....#.. +.........##.....#.#....#......# +..........#........#..#..#.#... +..#......#.....#......#......#. +..#...###..##..#.....##..#..##. +..#.#..###.........#.#...##.#.# +#.........#..#......#...##..... +...#...#.#..#...##.#...##.#..#. +#.....#.....#.##....#.#......#. +#....##..##..#.#..##....#.....# +.#..........#..#...#..#.......# +#.#.....#..##..##..#.#......... +....#..##...#.....#.....#.#.#.# +...#.#....#........#...#.#..... +.#............#.......#.##.#... +..##.......#.#...#........##..# +..................##.#...#.#..# +.#.........#.......#.....#..... +....##...##..#..........#...... +..#.##..#....#..#............#. +....####...#.##....##.#....#.## +#..#....#......##........##.... +..###...........##..#......#... +#..#.......#........#.......#.. +.....#....#..#..##.....#....... +.###.####.#....#....#..#....... +.............#...............#. +.#..........#.#....#..#.#...... +..............##....#..#....##. +.......#.#..#........#.......## +#..#...#..#.#........#..#....#. +...#.........#...#..#.......... +...#....##...#..#..........#... +.#......#......##..##...#.#.... +.#.........#..###.............. +.................#.#.....##.... +...#............#.............. +#..#................#.......#.. +...#.......#......#.#.#........ +#.....#.##....#.....#........#. +......##.#....#........#....#.. +.#..#.##...##........#.#.....#. +..#...#....#...#..#..##..#.#..# +#.................#.#.......##. +..........#........#.#.....#..# +#....##....#........##..##.#... +#...#....#.....#.....#.....#... +#..#..........#....##....#....# +..#.#..#..#....#.#....#....#..# +#....#..#.......#..........#... +.#...#.#...#..#...#.......#.... +###........#......##..#...##... +...#..........##..............# +.......#........##......#.....# +.#..........#...#...##....###.# +.#...#....#..#.....##...#..##.. +.#.#.#...##..........##...#...# +.#.....#...#........#........## +#.......#......##.#.#..#....#.. +##..#.##........#....#..#...... +...#.......................#... +..#....#..##........##.#.##.#.. +.............#.......#....#.#.# +...#...........##..#.....#..... +..#....##....#.....#........... +..#.....#......#..#.###.##....# +.#.......#...........#...#....# +#............##...#...#.....#.. +##...#.....#.........##...##... +...#...........#....##......... +#.##..#..........##..#......#.. +.......#.#.......##.......#.... +..#.....##..#...#.......#...... +.#........#....##...........#.. +#.......#...#.#.###...#....#... +..........##..#..#..##........# +#....#....#...#....#....#...... +...........#....#...#...##.#... +.........#.#.....#............. +..####...........##..........#. +.....#...................#..... +#..##...#........#.###.#.##.... +....##...#.##................#. +.#........###.#............#.#. +..............#.##.........#... +##............#.#..###....#...# +#.....#........####....#....##. +....##..#...##..##...##.....#.. +##..#....#.##.....####.....#.## +##..#....#.##.##.#.#........#.. +....#..........##.....#..#..#.. +...#.......#........#.........# +#..##.######.......##........#. +###...#...####.......#.....#... +#......#..#.....#......#.....#. +..................##...#....... +....#.#....#......#...#.....##. +..#..#..#..#..#....#.#...#....# +......#....###................. +#.##......#...#......#......... +#..#.#...##..#.......#..#...#.. +.#....#.#........#.........#... +#.......##..#..#............... +........#..##....#.....#..#.... +....#......##..#....#...#..#... +#.....#...##..#...#......#..... +.....#.....#.........##...#..#. +........#...##.#...#.#....#..## +....#....#...#.....##..#...#... +#....#..#.........#.........### +..###.....##...#.#....##......# +#..#.#..#.......#..#....##..... +###...#.##..#.......#......#... +.....#.....##.......#...##..#.. +......#.......#.#.#......#..#.. +.................##..#.###..... +..........#....#...#..........# +...#.#...#.#..##.....#.#.##..#. +.......#..#....#...#......###.. +...##..........#..#.....#....#. +.#..##..###...#....##.....#.... +..#.#..............#....#...#.. +.....####.......#.#.##....#.... +#.#.#..##.##.#..#.##.#....#..#. +........#....#.......##........ +...#...#....#...###.....###.... +.....#..#..........##.#...##.## +..#.#.#..#....#...#..##...#...# +..#......#..#.#.....#....#....# +.#.....#.......#............#.. +#..##....#...#....##....#...... +#..#.........#...#...###.#..#.. +..#.#.#..#.#..#.......##....... +...##...............#..#...#.#. +.......####.#.....#..#..#...... +......#..#.....#..##....#...... +....#...#.........##.......#.#. +#.#.#...#.....#...#..#.#..#.... +........#..#.........#..#..##.. +........###.#............#.#... +#..#.......#.#..#.......#...#.# +..##..............#.#.....#...# +..##........................... +..#.....#.......#......##...... +#...#......##.#....#.#.#...##.# +#...#.#......#.#..##.........#. +.##..#...#.#.....#.#.#...#.#..# +.#..#...#.#.........#......#... +...........#...#...#...#..#.#.. +.#........#...#......##...#.### +#........#..#.#..#...........## +.#...#...####.......#.......... +......#...............#........ +.....#.#.....#.#...#......#.... +.#........#...........#..##.#.. +....#..#.....###.......#...#... +#.#.........#...##..#.#.##.#... +................##.#....#.#...# +.......#.......#......#...#.... +#....#.#..............#.##..### +..##.##..#.....#............#.. +#....#..##........#....#....... +.#.#........#.#................ +......##..#..#..........#..#.#. +.....##.#..#....##.#......##... +........###.#................#. +#..###.....#.###.#...#.#....... +.#..#.#.#.#..#..#.#.....#.#.... +#....#.....#..#......##...#..## +........#...##..#.#.....#....#. +.......#..#..#..#....#.....##.. +....#..##..#...#....#.........# +#.#....#..#.#...#.#...#....#... +.....#......###.......#..##.#.# +.......##.....#....#........#.# +.##.##..#..###.#....#.#.....#.. +..##.#.......###.........#..... +.#...#......#..#....#.......... +.....#........#.....##...#..... +..#......#.#.#..#.#....##.#...# +#.#...#...........##......#.... +.................##.....#.#.##. +###..#....#..................#. +##..#.#.#...#....###.#.#...##.# +#.#.#..#....#..............#... +.....#....#......#..#.##....... +#...#...#..###.......#.......#. +.....#.#........#..#...#.#..... +.....#..........#.###.......#.. +...#.##.....#....###.....#..... +####........#....#..#.#.##.#... +#......#...##.....#.#..##.#.#.# +.....##....#..#.........##..... +..##....##................##..# +#.....#...##...##.#.....#...#.. +..#..#.#.#....#.#.......#...... +##.....##......#...#.........#. +#..........#........#.#......#. +.#..#.......#.#.....#.......... +.........#.#.......#.#..#..#..# +#......#....#....#..##..##...## +.....#..#...#.......#.....##... +..#.##........#.###...#...#...# +..#..#...........#..........#.. +.#.#.#...#.##.#..............#. +....#..##.......#.....#..##..#. +.#.##.#....##........#...##.##. +...#.#...#....#....#......##### +.....#.....##...........#...... +#........#.##.......#.#.......# +#...#.......##.#.......#..#.#.. +#...##..#....#............#.#.. +........#.#..#...#..#...##..##. +#...#....#............#........ +#.#.#.#.#....##.....##......... +......##.........#.......#.#..# +...#.#....#........#........... +...#.#.......#.....#........... +##....####......##.##..#....... +#......#...#..#.#..#......#..#. +#......#.#....#....#..#........ +..#.###...#.....#........#.#... +..#.....##.....###....#.....#.. +#.##.#.....##....#...###....... +###.#....###.#..##.#.......##.# +#..#..##...#.#..........##.##.. +.......####.#..#.....##..###... +#...#...##..#..##.......###.... +#....#.........##..#.........#. +.....#.#..........#..#...#.#..# +..........#......##..#..#.#.... +.#...#...#...#........###....## +#....#.##..........#.#.....#.#. +#....##.#.##..#.......#.#.....# +.##..##..#.#...#.#...........#. +....##..#...#.#.##.#.#...#..... +.#...#.##........#.##..#.#....# +.#.....##.........#.....#...... +..#.....#.#..#.##.............# +##....##...#....##........#.... +.#....#........#.#..#..#..#.##. +.#........#............#....... +.#..##..##..#..#..####....#.... +..#.###....#..#.##......#.#...# +.###..#.#...##....##....#..##.# +....##........#....#.#.#...##.. +...#..#....#.#....#...#.#.....# +...##....##..#....#.........#.. +.....#..##.###..#.....####..... +...#..#.........#....#.#.##..#. +...#..#...............#..#....# +...........#.....#...####..##.# +..#......#...#....#..#...##.#.. +.....#..#...........#.......#.# +##....###...#.........#....#... +...#..##.......#.#.....##....#. +#.#...#.#....#.....#...##.....# +.#...##....#.....#..##.......#. +...#........##..........#.....# +#...##..#.#....###...#..#...... +............#.......#......#.#. +......#....#.#...#...#..#...... +.#..#......#....#.......#....## +...#...#.......###..###...#.... +.............#.#...#..###.....# +.#.....#........#...##....#..#. +.....#.......#######.#.#...#... diff --git a/2020/day03_toboggan_trajectory/tests/sample_input b/2020/day03_toboggan_trajectory/tests/sample_input new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/day03_toboggan_trajectory/tests/sample_input @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.#