Add solution for 2020 day 03

This commit is contained in:
Patrick Auernig 2021-12-05 00:44:14 +01:00
parent c995c3c472
commit 2a6086f434
8 changed files with 437 additions and 2 deletions

View File

@ -6,7 +6,7 @@
| :-: | :----: | :----: | :------- |
| 01 | ✓ | ✓ | Rust |
| 02 | ✓ | ✓ | Rust |
| 03 | | | |
| 03 | ✓ | ✓ | Rust |
| 04 | | | |
| 05 | | | |
| 06 | | | |

2
2020/day-03/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
part_one
part_two

64
2020/day-03/common.rs Normal file
View File

@ -0,0 +1,64 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[derive(Debug)]
pub struct TreeMap {
pub width: usize,
pub height: usize,
pub map: Vec<bool>,
}
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<P: AsRef<Path>>(path: P) -> io::Result<TreeMap> {
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(),
})
}

View File

@ -0,0 +1,323 @@
.....#.##......#..##..........#
##.#.##..#............##....#..
......###...#..............#.##
.....#..##.#..#......#.#.#..#..
..#.......###..#..........#.#..
..#..#.##.......##.....#....#..
.##....##....##.###.....###..#.
..##....#...##..#....#.#.#.....
.....##..###.##...............#
#.....#..#....#.##...####..#...
#......#.#....#..#.##....#..#.#
##.#...#.#............#......#.
.#####.......#..#.#....#......#
..#.#....#.#.##...#.##...##....
.....#.#...#..####.##..#.......
#....#...##.#.#.##.#..##.....#.
##.##...#....#...#......#..##..
....##...#..#.#...#.#.#.....##.
..#....##......##....#.#....#..
#..#....#....###..#.##....#.#.#
..#.#####..##....#....#.....##.
.#...##.......#...#....#.#...##
#.#.#.##.......#.....#.#.#....#
.#.#.....#.......#.......##....
.#......#....#....#.......##...
#......#.....#......#..#..#....
#.#...#...#....##....#.#...#..#
....#.....##...#...#..#.#......
..#......#..........#...#.#....
..#..#......####..##...###.....
.#.....#...##...#.##........###
#.#....#..#....#..#.....#.#..#.
...##.##.#.#.##...#.....#......
##....#.#.#...####.#.#.#.#.....
.##.........#..#..###..........
..##.###.#..#..#....##.....#...
##........#..###....#.#..#..#..
....#.#.......##..#.#.#.#......
....##.....#.........##.......#
..#........##.#.........###..##
....#..................##..#...
#...#.#..###..#.....#..#..#...#
..#..#.##..#..#.......#.......#
.....#..##..#....##...........#
..##...#........#...#.#.......#
.........#.#..#.#..#.##.#.###..
....#...#..#..#......##....#.#.
..#..#.#....#....#..#.####..##.
##....#.....#......##.###.#..#.
#..#..##..###......#.#.#.#...#.
.......#..##..##...#...#..#....
..#.###.#...#....#.##.#.....##.
.#.#.......##...##...##....#...
#...#.#.#...#.####..#..##......
###..#.##..#..........#...#....
##.#.........#..##......####...
..##.#..#....#.##..............
...#....#.......###............
...#.....##....#.#.#.#.......##
###.###...#...#...###.##...##..
#.#....#.##..#.....#.....##.#..
...#....#....#.........#....#.#
##.#....#........#..#..##.#....
.#.#..#.......#...##.......#...
.##...##........#....#.#..#....
....#..#.##.###.....#.#........
.#.#...#.#..#.....#.........#..
.......#.#.#..##....#.........#
.##...#....#..#...#........#..#
....#....#..#.#..#.#.#....##.##
..##....#.....##..#.#...#...#..
#.##.........#.....#.......#.##
...#...##.#.#..........#......#
###...#.....#..#.......#####..#
#.####...##.#.#..#...#.........
.##.....#.....##..#...##.##....
.........###...#......##....###
.#....##...###.#..#...##..#.#.#
.......#.......#.#...##.#......
.....#.#........#..##.....##...
....#.#.........##.#...##..#.#.
#..#..#.##..#.##.##.....##.###.
..##.........###...#....#....#.
.###...#..#.##...........#.....
#..##..........#..........#....
.....#.#....#..##..#...#.#....#
..#.....#.#....#...##.##.......
##.....##........#....#..##....
.#..#.#.........#..#..#........
.............##....#....#..#...
....##....#..#.#.##....###.##.#
.###..#.....#..#..##..#..##..#.
...#..###.......#.#....#..###..
#.#..#.....#...#......#........
#..#..............###.#......#.
..#....##.#....#.##.#.#...#....
.........##..#...#.#.......#...
........#...#.#....#.....##..#.
...#.##..#..#..###..#..#......#
.....####......#...#....#...#.#
...###.#.#......#....#.......#.
#...##.#....#....##....##.###..
.......##...##.....#.##.#..#..#
.....#.#............##...#.####
.##..#.#.#.#..#.#.#.....#.##...
.#..####...#.#....#.....#..#...
....##..#.#...#..#....#.#......
...#......###..#..###..#.....#.
.#.#.#..##....#...##..#.....#..
###....#....#...##.....#...#...
#.##....#......#...###.........
.#..#.#...#..#....#....#....#..
...............##...####..#..#.
#.#...........####..#...##.....
##.#....#........#......#...##.
......#...#...#....#....#.....#
#......#.............#....###..
.#...#...##.....#...##.##..#...
..#.#......#.#........#........
.......#..#.#...##..#.#.#......
..##...#.##........#....#.#...#
.....#..#..#........#.#......##
....#.#...##............##....#
.#.#....#.#.#...#...#.##.....#.
#.#.##...#....#.#.#..#.##..#.#.
.........####..#...#...#.......
#..#..####......#..##..#...#...
.........##..................#.
.....##.#..##.#.#...#......##..
...#....#....#.#.....#...#..#.#
#...##.#...##...........#..#...
#..........#.#..#..#.##..#..#.#
.#...#.##...#.#.#..#.......##..
.........#...........#..#..#...
.##...##....#.#......#........#
#.#...........#....#.......#...
##.#.#.......#...###......##..#
...###..#.##..##.#.#.......#...
.#...#..##.#...#........#.....#
...#.......#..#..........#.#...
..#.#.#.#.....#.#.......#..#..#
#.##.....#..##...#..###.#....#.
.......#...........#...#....###
.......#..#...#.............#..
#.....###.......#...#........#.
.#..#..#..#...........#........
....#.#...#.#.##.#.#....#.##..#
.......#..##...##...#...#......
...#.....##.###...#.#...##....#
#..#....#...##......#....##....
#.#.......#....#.###.##..#..#..
..##...........#...#....#......
.#........#.....#..#..#...#..##
.....#.#.#..#.......#....#.....
#..#.#......#......##....#.....
##.....................##......
.##........###..#.........#...#
........#.........#..#.........
.#.##....#.....#...#.........##
....##......#.........#........
...#.#..#...##.##.#.#..####....
..##...........##.#.#....#.....
.#.....#.#...#..#.......#....#.
....#...#......##...#...##.#..#
....#..##....#..#.........##.#.
..##...##.##....#....##.###...#
..#....##..##.#.#.#...#......#.
##...#.........#...........#...
.##....##.#.....#...#.......#..
..........##.###.##....###....#
..........#..##..#....#.#.##.##
........##.#...#.#.#.#...###.#.
.#......#.#.#...###.#.#.#......
.........#......#......#...#..#
......#.....#.##....##.#####..#
..#..##...###.#..........#.#.#.
.#..#....###.#...#..#....#...##
...................#..........#
....###.....#...##......#.....#
#.....#..##.....#.#..........#.
..#.......##.#....#..#.##.#...#
........##.#..###..#......##...
#...........##.#...###..#....#.
....#...........#.....#.#...#..
.##..#.#...#...#.##...#..#.....
#........#.#.#.#.#.#...........
#..#.....#..#..#.##....#....#.#
..#............##....#.#.##...#
.....###.#....#.#......#.###...
...#.....#.#.................#.
..#...##..#.#...#...#...#.....#
.##.#........#..#....##..#..##.
.#..........#...#.#..#..#.#....
#.......##.........#.##..#.####
.#..............#.......##.....
#......#.##..........#..#......
..##...#...#.#...#............#
.##.##..##..##........##.....#.
.....#..#.....##...............
.#..#...##...#...#.....#.......
#......#...#.......#..##.###.##
###..##......##......###....#..
....#..........#...#.##.#.....#
.........#....#..#..#.#..##....
.....#.....#...........#......#
.#.......#...#....##...#.##...#
..##.#..............#..#...#.#.
.#..####.#.........#....#....#.
..###.#...#..#......#.......###
.#.#..##...###...#...#.#...#.#.
...#..##..###.#..#.....#.##....
#...###.#...##.....####.....#..
.#.##...#..#.#..##.....#.......
...#.##.....##.....#....#......
.#...##.....#..###..#..........
..........#...#.....#....##.#..
.......#...#...#...#........#..
#....##..#...#..##.#.#.....#...
.#.#..............#..#....#....
.####.#.#.###......#...#.#....#
.#...#...##.#...............#.#
...#.......##...#...#....##....
#..........###.##..........##.#
.......#...#....#.#..#.#....#..
....#.##.#...###..#..##.##.....
..#.#.#......#.#.......###.....
#..................#.##....#...
#.....#..#.#.#..#...#.........#
..#..#...#.#.##........#.......
#..#.#..#..........###...#.#...
.......#.##....#........##.#...
.####.#.#...#.#...##.##.....###
........#.#...#.#..##...##.....
....##.##......#.##.........#..
.#..#...#.#...........#........
.......#..........#....#...#...
..###.#.###..#..#.....#..##....
.#..........#.......##...#.....
.#.....#...#........#...#.##..#
.#..#.......#..#.......#.#.#...
....#..##.#...##...#.#....#....
.....#.........#..#..#....#....
..#.#..##....#..#..##.#.#.....#
........#.#...###....#.#.#.....
.#.....#.......#..###.#........
.......#...#.#...#...##........
##.............#.#.....#.#..#..
.#....#.......#.#.......#..##..
#.....#........#..##..##.......
...........#.........###......#
....#.##...#.#...#...#....#..##
......#..##......#......#.##.#.
......##....####...###...#.....
#....#..........#.#.##.....#..#
....#.#...........#.#.#.#.#...#
....####.....##...#..##..#...#.
#....#.###..###.....#..###.....
..##.........#......#...##.#...
..#.....#.#...#.##.#...#..###.#
..#.##..##........#.......#.###
.....#..........#.....#....#...
.......##..##..###.......#####.
..###......#.#....###....##...#
#..##.....#..###...#.....##.##.
#..#..##.##.###.####.##.#......
.#.#......#.##......#..#......#
..###.....#.#......#.#.####....
#..............#..#.#...#.###..
...#..#.##..........##.#...#.##
.#.#.#.........#....#.#..#.....
..#.##..#...#..#...#......#....
.......#...#.##.#.#..#...##..#.
..........#.####...#........#.#
....#...#....##.#.........#.#..
##.#..#.......###....#..#..#.#.
..##.....#..#.#.#.####......#..
.#.....#..........#..#..#.#....
......#.#.......#.#...#..#..#..
...#...............#....#...#..
##.......#.........#.......#...
...#.......###...#.#...#.......
#...###....#....##....#....#...
...#....##..#.#.............##.
.....#.#.#..#......#...#.#..#..
.##....#..##..#####..##.....##.
....##.#.#..#.....#.#...#......
...#.....##.#.#..##..#.#.......
.......#..#..#..........#......
.......#...#..#.........#.##...
..#..#..#...##..#.#....#......#
..#....#...#.#......#........#.
.#...#..#...#.#..........#.....
#..#...####..#......##.##.#.#..
.#...#.#...#.#.....#..##.#.....
..#.##.#......#.........##...#.
###..............#.............
...#...###....#..#.............
.##....#......#..#.....#..#..#.
.#..........#.....##...#..#....
....##..#.#......###.##......#.
.#..##.#.##.#...##.#......###.#
#..###.#...###..#........#.#...
#..#.#.#..#...###.##.##..#..#..
#.#..#....#.........##......#..
....###.....###....#...........
....#..##.##....##..#.....#....
.#.....#....####...#..##.#..###
.........##..#......#.#...##...
.##.......#.....#.###.#..#.#..#
.....#.#...###.....#......####.
##.#...#......#.#.#..#.####...#
.#.##.....#..#..#.............#
.#..###..#..#......#..##......#
.......#.#........##.....#.#...
#....#...#..###..#.#.....#.##..
.##.....#.#....###..#.....##...
...##....#....#...#....#.#.#...
#####..#...........###....#...#
.#.......##.##.....#....#......
.#..#.#...#..#......#...#..#.#.
....#.....##...#####..#...#...#
###.##...#.#............#....#.
.....#...#........##.........#.

View File

@ -0,0 +1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

16
2020/day-03/part_one.rs Normal file
View File

@ -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(())
}

19
2020/day-03/part_two.rs Normal file
View File

@ -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(())
}

View File

@ -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)