commit bc3de2d4fbe868f2e4982985f10bc66f7af40818 Author: LongHairedHacker Date: Mon Dec 7 17:10:49 2020 +0100 Solution for problem 3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..52c8238 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "AoC2020" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0ebbad3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "AoC2020" +version = "0.1.0" +authors = ["LongHairedHacker "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/inputs/problem3.txt b/inputs/problem3.txt new file mode 100644 index 0000000..6e5de58 --- /dev/null +++ b/inputs/problem3.txt @@ -0,0 +1,323 @@ +...........#..#.#.###....#..... +...#..#...........#.#...#...... +#.....#..#........#...#..##.... +..#...##.#.....#.............#. +#.#..#......#.....#....#....... +.....#......#..#....#.....#.... +.......#.#..............#...... +.....#...#..........##...#..... +...#....#.#...#.#........#...#. +..#.........###.......##...##.. +.#....#...........#........#..# +..#.............##............. +..#.##.#....#................#. +.....##.#.......#....#...#..... +......#.#....##................ +..#..........###..#..#.#..#.... +....#..............#....##..#.# +.#.........#.#....#.#.#....#... +..#.....#......##.#....#....... +..#.#....#..#.#...##....###.... +...#......##...#........#.#..#. +.##.#.......##....#............ +...##..#.#............#...#.#.. +.##...##.#..#.................. +..#......##......#......##..... +.....##...#..#...#.........#... +.##.#.....#..#..#.##....##....# +..#.#......#.......##.......... +......................#......## +##.#...#.................#.#.#. +......#.#..........#.....##.#.. +#.#......#.....#...........#... +.....#...#.......#..#..#.#...#. +...........#......#.#...#...... +....##...##...........#......#. +.........#.##.................. +......#...#....#......##.##...# +......#...#.#########......#... +.......#.#...#.......#..#...... +............#...#...#.###...##. +...........#..........#...#.... +...#..#.#................#.#..# +..#....#.....#.............#.#. +....##......#........#....#.... +........##...............#....# +........#..#...#..............# +...#....#.#...#..#...#....#.#.# +.........#.......#....##....... +#.##..............#.#........## +......................###...... +.........#....##..##....#.#.#.. +.#...##.#.#...#....##..#.....#. +....................#.#......#. +.#..#.......................#.. +..#..#.............#..#....#... +...#...#..#...#...#.#..#.##.... +........#....#...#....#........ +.#.....#........#.........#...# +...#......#..#..#..####..#..... +#....#..............#.##....... +.#....#.............##...#..... +....#...#.##........##......#.. +##....#...#.......#..#........# +....##........................# +..................#..#......... +..#....#........#..#.......#... +#...#..#....#...##...........#. +.........#..#..#.#.##.......... +....#.#..#.....#..#.#.#.#..#.## +##................#.##.....#... +.#.....###..#..#..#.....#....## +...#...........#..........####. +.#.....#....#......#.##..#.#... +..#...##....#................#. +........#.......#......#.#..... +....#.#.#.#....#...#......#..#. +...........#......#..#......... +###...##......##.#..#....##.... +##....##.........#..#....###... +#.#.....#....#......#...#..##.. +#....##.#..............#.##.... +.#........#.#.........#...#.... +......................#......#. +........#..#..##.....#..#.#.... +..#...###.................#..#. +...#...#............#.......... +.##.......#..#.........#....#.. +.#..............#....#....##... +...............##..#.#.......## +.#.....#....#...#..#.......#..# +#..#.............#....#......#. +.....#.#......#.........###..#. +.#...#.#...............#....#.. +#......#.............#......... +.#.##.#.####...#..#.##..#.....# +.....#......#..#...#.......#... +#........###...#.....#..#.....# +....#.#.....#...#..........#... +...#.#.......#.#......#..#.##.. +..#..........#.#..#.......#.#.. +#...#.#..............#...###.#. +...#..#...#............###..... +..#..#...#.#............#..#... +.###.#.....................#..# +....#....#..#.....##.##........ +#....#....#.#..#.........#..... +.#.....##....#............##..# +#....#.#...#...#..#.#......#... +#.....##.....##.....##.#...##.. +...##...#..#..####.#........#.. +.........#...#......##..##..#.. +..#.....###.#..#..####.#....... +.......#......#...#..##....#... +.#.....#.#.#..#....#...#..##... +..........#.#...#...#.#..#..... +....#.....#........#.....##..#. +..#.#.##.........#...##.....##. +.........#.##....#............# +............##.....#.......#.#. +......#.....#...#..#..###...... +##.....#.......#...##.#....#... +...........##......#..##...#.#. +..#.#.#.#...#.......#....#...#. +#.............#.....#.#...###.. +##....#.......#.....#..##.#.... +...#.......#....#.........##... +......#.......#......##.##..... +..#......#...#.#........#...... +....#.#....#.#.##......#.....#. +#......#.........#..#....#..... +........#..#....##.....#....... +#......##....#.##....#......#.. +..#.......#............##.....# +...........#...#...........#... +#.......#...#....#....#...#.#.# +..###..#........#........#..... +..#..###...........#.#......#.. +.#...........#......#.......... +.#.......#.....#.......#....... +..#......##.#............#....# +#..........#.....#.#..#........ +.....#...##.##.......#......#.. +..........#.....#........#.#.#. +....#......#.....#......#.#.... +.........#.#.#..#...##....#...# +.........#.......#...##...#.#.. +.##........#...............#... +.......#....#...........##..... +.........###......#.........#.# +......#.......#...#..........#. +...#.#..........##......#...#.. +#.......#.#..........#......... +................#..#......#..## +.....#...#....#.#.....#........ +#.....#....#...........#....#.. +#....#.#..#...##....#...##.#... +...#.....#......#.#....#..#..#. +..#................#...#.#..##. +..........#..............#..#.# +.....##.....#..#.###........... +....#.#......#.#...........#... +.#....#.#.........##.#....#.... +.#.#........#........###....#.. +##.#................#...#..#... +.......#......##..#.....#..#.#. +...#............#......###...## +#.#...........#.........#...... +.....#.#.#.................#... +....#..............#...#.#..... +...#.#.....##..#............... +.#..##...#....##.....###..#.... +...............#...#...#.#.###. +.###....#.....#...#.#......#... +...#..#.....#.......#..##.#.... +...........#..#....##..#...#... +...#...#..........#.......##.#. +............#.#.......#........ +....#.........#.....#.......... +...#.###.##..#...##..####..#..# +.#.#...#..#...................# +.....#..#.....##..#............ +....#......#...##..#.##........ +...#...............#..#.....##. +...#......#.........#.#..##.... +.#....#.##.......#......#...... +....#.......#....#..........#.. +#.#.#....###.#.#.............#. +..##..###........#.#..#.#..#... +......#.#............##.#...### +.........#.#....#####.....##... +............##......#.#..#..... +...#.....#.....###....#........ +##..........####.##.#.#........ +....................##.....##.# +#.#............#........#...... +....#...##.....#......#....#... +...###.#..##..................# +..###......#..............#.#.# +.#...#...........#....#........ +....#............#..#..#.#..... +...#.........#.#.........#.#### +..#...#...#...#...........#.... +...............#.#......##..#.. +#....#.#.......#.#..#......#..# +........#.#....#..#...#..#...#. +...#..#.......#...........#.... +...........#.......#........... +.#......#................#..... +....#.#.#...#......#..#...#.... +................#.#.#....#..... +.........................##..#. +.#...........#............##... +#...............#.....##.#.#..# +.........#.......###....#.....# +....##...#...#.....#..#........ +........#.....#..#.#.#...#..#.. +......#.......#.#.........#.#.. +#......#............#...#....#. +#..##...#..#................#.. +.##...#...#.....#.##.......#..# +.......#.##........##..##...... +##.#..##...............#.....#. +......#....#..##...#......###.# +#........##..#....#.#......#... +.#......##.#...#.#...#......... +.#.#...#..#.............#...... +.##..........#..........#...... +.#.....#.....#..............#.# +..#.........#..#.#.....#.#....# +..#.##..............##...#..### +....................#.......... +......###..#..#...........#.... +..#..........#.......#...#..... +...#......#......#............. +....##..............#.#.....#.. +........#.#......#..#........## +.............#...#.#.........## +...###...#..........##.......#. +.#..........#...##..#.#.....#.. +##...#.........#............... +......#....#....#.....#.....#.. +..........#....#...#...#..#...# +...##....#.#.#..#...##......... +#......#.#...##.###...#....#... +##.......##.#......##..#...#... +......#.............#.##.....## +#.......###....####.#...##....# +..#...#..#.......#..........#.. +#.....#..#..#..#.##...###...#.. +.....##.#..#..#..#.#....#...#.. +..#...#..................##.... +....#.#........##.............. +#...#.......##...#...#.#....... +..#...#........##....#.#....... +..........###...###...#......#. +#.....#..###...##...##..#..#..# +..#.....##.....#.......##..#.#. +........#........#.........#... +.................#....#.......# +.......#...#.....#...#.#....... +....##...............#...##...# +.##...#................#...#... +.............#................. +.#..#....#....#.#....#......... +.#.#..#..........#.......#..... +.....##.....##...#..#.......... +#...#.#.........#......#..#.... +........#....#...#....#.#.##... +....#..#........#...#...#...... +.#..#.....#.#...#.........#.... +.#..#..#...........#..#....#... +....###.............#..#....... +#......#..#..##..........#.#... +#..#..#.##..#...#.#.#.......... +....###......#.##.....#....#... +.##..#...#......##.#........... +..#..#.......#.....#.##....#.#. +.......#.#.#........#....##.... +..##...#....#...............### +#..##..#...........#.#....##... +...##..#.....................#. +###......#....#....###..#...##. +.........##............#..#...# +..#..........#...#.#.#......#.# +.......#.....##..##......#.##.. +#..........#.....##.#.......... +#.......#.#...#...#....#....... +#...#.....##.......#.#..#.#.#.. +.........#.#.#..#..#...#.###... +.................##...#....#... +###.......#..........##...#.... +#.#..#.........#....##.#....... +......#.#.....#........#....... +.......#.#........#......#.#..# +..............#..#...##....#..# +#...........#...##.....#..#.#.. +..#....#..#.#.#...#..#....#.#.. +...##.#.....#..#...##..#.....#. +..#.#................#........# +......#...#.............#...... +.##............#....#...#..#... +....#...#...........#.......#.. +.###..#.......#.............#.# +.#.#....#.#...........#.#...... +...#.........#.........#..#.... +...#..........#.#.....#.#...... +.....#........#....##......#... +..#.#.#......#..#.#......#....# +.#.#..#................#.#..... +.#.#.........##...#.......#.#.# +#..#.....#...#..#...........#.. +..##......####......#..#....### +#.....###....#.#........#..#..# +..##.#...#.#..##..........#..#. +#.........#.#.............#...# +...#.#...#...#.#.#....##....... +##.##...#.....#...#...........# +....#........#.#.....#......... +.................##..#..##...## +.....##....#...#...#.....#..#.. +....#...#........#............# +..#...........##....#...#...##. +.....#......#.........#..##.#.. diff --git a/src/bin/problem3.rs b/src/bin/problem3.rs new file mode 100644 index 0000000..ee9ad65 --- /dev/null +++ b/src/bin/problem3.rs @@ -0,0 +1,43 @@ +use std::error::Error; +use std::fs::File; +use std::io::{self, BufRead}; +use std::vec::Vec; + +fn main() -> Result<(), Box> { + let file = File::open("inputs/problem3.txt")?; + let lines = io::BufReader::new(file).lines(); + + let trees: Vec> = lines + .map(|l| l.unwrap().chars().map(|c| c == '#').collect()) + .collect(); + + let width = trees[0].len(); + let height = trees.len(); + + println!("Map dimensions {} x {}", width, height); + + let slopes = vec![(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]; + + let mut overall = 1u32; + + for (x_step, y_step) in slopes.iter() { + let mut x = 0; + let mut y = 0; + let mut count = 0; + + while y < height { + if trees[y][x % width] { + count += 1 + } + x += x_step; + y += y_step; + } + + println!("Count for {} {} : {}", x_step, y_step, count); + overall *= count; + } + + println!("Final result: {}", overall); + + Ok(()) +}