diff --git a/2020/README.md b/2020/README.md index 05c9082..0f8e64e 100644 --- a/2020/README.md +++ b/2020/README.md @@ -6,7 +6,7 @@ | :-: | :----: | :----: | :------- | | 01 | ✓ | ✓ | Rust | | 02 | ✓ | ✓ | Rust | -| 03 | | | | +| 03 | ✓ | ✓ | Rust | | 04 | | | | | 05 | | | | | 06 | | | | diff --git a/2020/day-03/.gitignore b/2020/day-03/.gitignore new file mode 100644 index 0000000..f47240e --- /dev/null +++ b/2020/day-03/.gitignore @@ -0,0 +1,2 @@ +part_one +part_two diff --git a/2020/day-03/common.rs b/2020/day-03/common.rs new file mode 100644 index 0000000..364b22b --- /dev/null +++ b/2020/day-03/common.rs @@ -0,0 +1,64 @@ +use std::fs::File; +use std::io::{self, Read}; +use std::path::Path; + +pub type Result = std::result::Result>; + +#[derive(Debug)] +pub struct TreeMap { + pub width: usize, + pub height: usize, + pub map: Vec, +} + +impl TreeMap { + pub fn pos_has_tree(&self, x: usize, y: usize) -> bool { + let pos = x + (y * self.width); + self.map[pos] + } + + pub fn find_in_slope(&self, dx: usize, dy: usize) -> u64 { + let mut tree_count = 0; + let mut x = 0; + let mut y = 0; + + loop { + y += dy; + + if y >= self.height { + break; + } + + x += dx; + x = if x >= self.width { x - self.width } else { x }; + + if self.pos_has_tree(x, y) { + tree_count += 1; + }; + } + + tree_count + } +} + +pub fn read_file>(path: P) -> io::Result { + let mut file = File::open(path)?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + + let width = content.lines().next().unwrap().len(); + let height = content.lines().count(); + + Ok(TreeMap { + width, + height, + map: content + .chars() + .filter_map(|ch| match ch { + '#' => Some(true), + '.' => Some(false), + _ => None, + }) + .collect(), + }) +} diff --git a/2020/day-03/inputs/puzzle.txt b/2020/day-03/inputs/puzzle.txt new file mode 100644 index 0000000..9ba5fa1 --- /dev/null +++ b/2020/day-03/inputs/puzzle.txt @@ -0,0 +1,323 @@ +.....#.##......#..##..........# +##.#.##..#............##....#.. +......###...#..............#.## +.....#..##.#..#......#.#.#..#.. +..#.......###..#..........#.#.. +..#..#.##.......##.....#....#.. +.##....##....##.###.....###..#. +..##....#...##..#....#.#.#..... +.....##..###.##...............# +#.....#..#....#.##...####..#... +#......#.#....#..#.##....#..#.# +##.#...#.#............#......#. +.#####.......#..#.#....#......# +..#.#....#.#.##...#.##...##.... +.....#.#...#..####.##..#....... +#....#...##.#.#.##.#..##.....#. +##.##...#....#...#......#..##.. +....##...#..#.#...#.#.#.....##. +..#....##......##....#.#....#.. +#..#....#....###..#.##....#.#.# +..#.#####..##....#....#.....##. +.#...##.......#...#....#.#...## +#.#.#.##.......#.....#.#.#....# +.#.#.....#.......#.......##.... +.#......#....#....#.......##... +#......#.....#......#..#..#.... +#.#...#...#....##....#.#...#..# +....#.....##...#...#..#.#...... +..#......#..........#...#.#.... +..#..#......####..##...###..... +.#.....#...##...#.##........### +#.#....#..#....#..#.....#.#..#. +...##.##.#.#.##...#.....#...... +##....#.#.#...####.#.#.#.#..... +.##.........#..#..###.......... +..##.###.#..#..#....##.....#... +##........#..###....#.#..#..#.. +....#.#.......##..#.#.#.#...... +....##.....#.........##.......# +..#........##.#.........###..## +....#..................##..#... +#...#.#..###..#.....#..#..#...# +..#..#.##..#..#.......#.......# +.....#..##..#....##...........# +..##...#........#...#.#.......# +.........#.#..#.#..#.##.#.###.. +....#...#..#..#......##....#.#. +..#..#.#....#....#..#.####..##. +##....#.....#......##.###.#..#. +#..#..##..###......#.#.#.#...#. +.......#..##..##...#...#..#.... +..#.###.#...#....#.##.#.....##. +.#.#.......##...##...##....#... +#...#.#.#...#.####..#..##...... +###..#.##..#..........#...#.... +##.#.........#..##......####... +..##.#..#....#.##.............. +...#....#.......###............ +...#.....##....#.#.#.#.......## +###.###...#...#...###.##...##.. +#.#....#.##..#.....#.....##.#.. +...#....#....#.........#....#.# +##.#....#........#..#..##.#.... +.#.#..#.......#...##.......#... +.##...##........#....#.#..#.... +....#..#.##.###.....#.#........ +.#.#...#.#..#.....#.........#.. +.......#.#.#..##....#.........# +.##...#....#..#...#........#..# +....#....#..#.#..#.#.#....##.## +..##....#.....##..#.#...#...#.. +#.##.........#.....#.......#.## +...#...##.#.#..........#......# +###...#.....#..#.......#####..# +#.####...##.#.#..#...#......... +.##.....#.....##..#...##.##.... +.........###...#......##....### +.#....##...###.#..#...##..#.#.# +.......#.......#.#...##.#...... +.....#.#........#..##.....##... +....#.#.........##.#...##..#.#. +#..#..#.##..#.##.##.....##.###. +..##.........###...#....#....#. +.###...#..#.##...........#..... +#..##..........#..........#.... +.....#.#....#..##..#...#.#....# +..#.....#.#....#...##.##....... +##.....##........#....#..##.... +.#..#.#.........#..#..#........ +.............##....#....#..#... +....##....#..#.#.##....###.##.# +.###..#.....#..#..##..#..##..#. +...#..###.......#.#....#..###.. +#.#..#.....#...#......#........ +#..#..............###.#......#. +..#....##.#....#.##.#.#...#.... +.........##..#...#.#.......#... +........#...#.#....#.....##..#. +...#.##..#..#..###..#..#......# +.....####......#...#....#...#.# +...###.#.#......#....#.......#. +#...##.#....#....##....##.###.. +.......##...##.....#.##.#..#..# +.....#.#............##...#.#### +.##..#.#.#.#..#.#.#.....#.##... +.#..####...#.#....#.....#..#... +....##..#.#...#..#....#.#...... +...#......###..#..###..#.....#. +.#.#.#..##....#...##..#.....#.. +###....#....#...##.....#...#... +#.##....#......#...###......... +.#..#.#...#..#....#....#....#.. +...............##...####..#..#. +#.#...........####..#...##..... +##.#....#........#......#...##. +......#...#...#....#....#.....# +#......#.............#....###.. +.#...#...##.....#...##.##..#... +..#.#......#.#........#........ +.......#..#.#...##..#.#.#...... +..##...#.##........#....#.#...# +.....#..#..#........#.#......## +....#.#...##............##....# +.#.#....#.#.#...#...#.##.....#. +#.#.##...#....#.#.#..#.##..#.#. +.........####..#...#...#....... +#..#..####......#..##..#...#... +.........##..................#. +.....##.#..##.#.#...#......##.. +...#....#....#.#.....#...#..#.# +#...##.#...##...........#..#... +#..........#.#..#..#.##..#..#.# +.#...#.##...#.#.#..#.......##.. +.........#...........#..#..#... +.##...##....#.#......#........# +#.#...........#....#.......#... +##.#.#.......#...###......##..# +...###..#.##..##.#.#.......#... +.#...#..##.#...#........#.....# +...#.......#..#..........#.#... +..#.#.#.#.....#.#.......#..#..# +#.##.....#..##...#..###.#....#. +.......#...........#...#....### +.......#..#...#.............#.. +#.....###.......#...#........#. +.#..#..#..#...........#........ +....#.#...#.#.##.#.#....#.##..# +.......#..##...##...#...#...... +...#.....##.###...#.#...##....# +#..#....#...##......#....##.... +#.#.......#....#.###.##..#..#.. +..##...........#...#....#...... +.#........#.....#..#..#...#..## +.....#.#.#..#.......#....#..... +#..#.#......#......##....#..... +##.....................##...... +.##........###..#.........#...# +........#.........#..#......... +.#.##....#.....#...#.........## +....##......#.........#........ +...#.#..#...##.##.#.#..####.... +..##...........##.#.#....#..... +.#.....#.#...#..#.......#....#. +....#...#......##...#...##.#..# +....#..##....#..#.........##.#. +..##...##.##....#....##.###...# +..#....##..##.#.#.#...#......#. +##...#.........#...........#... +.##....##.#.....#...#.......#.. +..........##.###.##....###....# +..........#..##..#....#.#.##.## +........##.#...#.#.#.#...###.#. +.#......#.#.#...###.#.#.#...... +.........#......#......#...#..# +......#.....#.##....##.#####..# +..#..##...###.#..........#.#.#. +.#..#....###.#...#..#....#...## +...................#..........# +....###.....#...##......#.....# +#.....#..##.....#.#..........#. +..#.......##.#....#..#.##.#...# +........##.#..###..#......##... +#...........##.#...###..#....#. +....#...........#.....#.#...#.. +.##..#.#...#...#.##...#..#..... +#........#.#.#.#.#.#........... +#..#.....#..#..#.##....#....#.# +..#............##....#.#.##...# +.....###.#....#.#......#.###... +...#.....#.#.................#. +..#...##..#.#...#...#...#.....# +.##.#........#..#....##..#..##. +.#..........#...#.#..#..#.#.... +#.......##.........#.##..#.#### +.#..............#.......##..... +#......#.##..........#..#...... +..##...#...#.#...#............# +.##.##..##..##........##.....#. +.....#..#.....##............... +.#..#...##...#...#.....#....... +#......#...#.......#..##.###.## +###..##......##......###....#.. +....#..........#...#.##.#.....# +.........#....#..#..#.#..##.... +.....#.....#...........#......# +.#.......#...#....##...#.##...# +..##.#..............#..#...#.#. +.#..####.#.........#....#....#. +..###.#...#..#......#.......### +.#.#..##...###...#...#.#...#.#. +...#..##..###.#..#.....#.##.... +#...###.#...##.....####.....#.. +.#.##...#..#.#..##.....#....... +...#.##.....##.....#....#...... +.#...##.....#..###..#.......... +..........#...#.....#....##.#.. +.......#...#...#...#........#.. +#....##..#...#..##.#.#.....#... +.#.#..............#..#....#.... +.####.#.#.###......#...#.#....# +.#...#...##.#...............#.# +...#.......##...#...#....##.... +#..........###.##..........##.# +.......#...#....#.#..#.#....#.. +....#.##.#...###..#..##.##..... +..#.#.#......#.#.......###..... +#..................#.##....#... +#.....#..#.#.#..#...#.........# +..#..#...#.#.##........#....... +#..#.#..#..........###...#.#... +.......#.##....#........##.#... +.####.#.#...#.#...##.##.....### +........#.#...#.#..##...##..... +....##.##......#.##.........#.. +.#..#...#.#...........#........ +.......#..........#....#...#... +..###.#.###..#..#.....#..##.... +.#..........#.......##...#..... +.#.....#...#........#...#.##..# +.#..#.......#..#.......#.#.#... +....#..##.#...##...#.#....#.... +.....#.........#..#..#....#.... +..#.#..##....#..#..##.#.#.....# +........#.#...###....#.#.#..... +.#.....#.......#..###.#........ +.......#...#.#...#...##........ +##.............#.#.....#.#..#.. +.#....#.......#.#.......#..##.. +#.....#........#..##..##....... +...........#.........###......# +....#.##...#.#...#...#....#..## +......#..##......#......#.##.#. +......##....####...###...#..... +#....#..........#.#.##.....#..# +....#.#...........#.#.#.#.#...# +....####.....##...#..##..#...#. +#....#.###..###.....#..###..... +..##.........#......#...##.#... +..#.....#.#...#.##.#...#..###.# +..#.##..##........#.......#.### +.....#..........#.....#....#... +.......##..##..###.......#####. +..###......#.#....###....##...# +#..##.....#..###...#.....##.##. +#..#..##.##.###.####.##.#...... +.#.#......#.##......#..#......# +..###.....#.#......#.#.####.... +#..............#..#.#...#.###.. +...#..#.##..........##.#...#.## +.#.#.#.........#....#.#..#..... +..#.##..#...#..#...#......#.... +.......#...#.##.#.#..#...##..#. +..........#.####...#........#.# +....#...#....##.#.........#.#.. +##.#..#.......###....#..#..#.#. +..##.....#..#.#.#.####......#.. +.#.....#..........#..#..#.#.... +......#.#.......#.#...#..#..#.. +...#...............#....#...#.. +##.......#.........#.......#... +...#.......###...#.#...#....... +#...###....#....##....#....#... +...#....##..#.#.............##. +.....#.#.#..#......#...#.#..#.. +.##....#..##..#####..##.....##. +....##.#.#..#.....#.#...#...... +...#.....##.#.#..##..#.#....... +.......#..#..#..........#...... +.......#...#..#.........#.##... +..#..#..#...##..#.#....#......# +..#....#...#.#......#........#. +.#...#..#...#.#..........#..... +#..#...####..#......##.##.#.#.. +.#...#.#...#.#.....#..##.#..... +..#.##.#......#.........##...#. +###..............#............. +...#...###....#..#............. +.##....#......#..#.....#..#..#. +.#..........#.....##...#..#.... +....##..#.#......###.##......#. +.#..##.#.##.#...##.#......###.# +#..###.#...###..#........#.#... +#..#.#.#..#...###.##.##..#..#.. +#.#..#....#.........##......#.. +....###.....###....#........... +....#..##.##....##..#.....#.... +.#.....#....####...#..##.#..### +.........##..#......#.#...##... +.##.......#.....#.###.#..#.#..# +.....#.#...###.....#......####. +##.#...#......#.#.#..#.####...# +.#.##.....#..#..#.............# +.#..###..#..#......#..##......# +.......#.#........##.....#.#... +#....#...#..###..#.#.....#.##.. +.##.....#.#....###..#.....##... +...##....#....#...#....#.#.#... +#####..#...........###....#...# +.#.......##.##.....#....#...... +.#..#.#...#..#......#...#..#.#. +....#.....##...#####..#...#...# +###.##...#.#............#....#. +.....#...#........##.........#. diff --git a/2020/day-03/inputs/sample.txt b/2020/day-03/inputs/sample.txt new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/day-03/inputs/sample.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/2020/day-03/part_one.rs b/2020/day-03/part_one.rs new file mode 100644 index 0000000..893be90 --- /dev/null +++ b/2020/day-03/part_one.rs @@ -0,0 +1,16 @@ +mod common; + +use std::env; + +use common::{read_file, Result}; + +fn main() -> Result<()> { + let path = env::args().skip(1).next().unwrap(); + let map_of_trees = read_file(path)?; + + let tree_count = map_of_trees.find_in_slope(3, 1); + + println!("{}", tree_count); + + Ok(()) +} diff --git a/2020/day-03/part_two.rs b/2020/day-03/part_two.rs new file mode 100644 index 0000000..44376ec --- /dev/null +++ b/2020/day-03/part_two.rs @@ -0,0 +1,19 @@ +mod common; + +use std::env; + +use common::{read_file, Result}; + +fn main() -> Result<()> { + let path = env::args().skip(1).next().unwrap(); + let map_of_trees = read_file(path)?; + + let result: u64 = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] + .iter() + .map(|(x, y)| map_of_trees.find_in_slope(*x, *y)) + .product(); + + println!("{}", result); + + Ok(()) +} diff --git a/README.md b/README.md index 856ca40..c47ac81 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,5 @@ - [2017](2017/README.md) (0% completed) - [2018](2018/README.md) (0% completed) - [2019](2019/README.md) (0% completed) -- [2020](2020/README.md) (4% completed) +- [2020](2020/README.md) (12% completed) - [2021](2021/README.md) (16% completed)