Added Solution for 2020 day 03
This commit is contained in:
parent
79ad34fc6a
commit
0c1d65edfc
5 changed files with 476 additions and 0 deletions
8
2020/day03_toboggan_trajectory/Cargo.toml
Normal file
8
2020/day03_toboggan_trajectory/Cargo.toml
Normal file
|
@ -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]
|
66
2020/day03_toboggan_trajectory/challenge.txt
Normal file
66
2020/day03_toboggan_trajectory/challenge.txt
Normal file
|
@ -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:
|
68
2020/day03_toboggan_trajectory/src/lib.rs
Normal file
68
2020/day03_toboggan_trajectory/src/lib.rs
Normal file
|
@ -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<Vec<bool>>,
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Grid {
|
||||
type Error = ParseError;
|
||||
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
trees: value.lines().map(|line| line.chars().map(|c| match c {
|
||||
'.' => Ok(false),
|
||||
'#' => Ok(true),
|
||||
_ => Err(ParseError::InvalidChar(c)),
|
||||
}).collect::<Result<Vec<_>, _>>()).collect::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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::<usize>() * 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)));
|
||||
}
|
||||
}
|
323
2020/day03_toboggan_trajectory/tests/challenge_input
Normal file
323
2020/day03_toboggan_trajectory/tests/challenge_input
Normal file
|
@ -0,0 +1,323 @@
|
|||
.#...#.......#...#...#.#.#.....
|
||||
####.....#.#..#...#...........#
|
||||
.....#...........#......#....#.
|
||||
......#..#......#.#..#...##.#.#
|
||||
............#......#...........
|
||||
...........#.#.#....#.......##.
|
||||
....#.......#..............#...
|
||||
........##...#.#.....##...##.#.
|
||||
.#.#.....##................##..
|
||||
.##................##..#...##..
|
||||
....#...###...##.........#....#
|
||||
.##......#.........#...........
|
||||
...#.#.#....#....#...#...##...#
|
||||
..#....##...#..#.#..#.....#.#..
|
||||
.......#...#..#..#.....#...#..#
|
||||
.....#......#.......#.....#.#..
|
||||
....#..#...#..#####....##......
|
||||
.#...........#......#....#....#
|
||||
#......#.###.....#....#....#...
|
||||
....#..#.#.#..#...........##...
|
||||
..#..#..#.#...#......#....#.##.
|
||||
.##....#......#...#.#..#.......
|
||||
..###.#...#.........#.#.#...#.#
|
||||
#....###.........#...#...#...#.
|
||||
...##.#............#...##......
|
||||
...#.........#............#....
|
||||
......##...#...##..#...........
|
||||
........##..#.#.####...#.....#.
|
||||
.##.........#......#..#..#...#.
|
||||
..........#...#..........#.....
|
||||
#..........#........#..#..#.#..
|
||||
..#....#.#.#.#.#..#.##.........
|
||||
##.#.#.##.....#..#......###....
|
||||
##....#...#.....#..............
|
||||
.#..#...#...#....###......#....
|
||||
#....#......#.#..#.#........###
|
||||
.#....#..#...###....#...##.....
|
||||
.#....#.....#.....#..##..#.....
|
||||
#....#.##...#...#..#.##.##.#...
|
||||
.#.#.#.##...#####.............#
|
||||
......##..#.....##..#...####...
|
||||
#.##..#.#....#..##.......###..#
|
||||
..#.......##....#........#.##..
|
||||
#.....#......#.....#....#..#...
|
||||
.......##...#.....##.......#..#
|
||||
.......#...#.#.#.........#####.
|
||||
#.......#.##..##........##.....
|
||||
##..#...#........##....#.......
|
||||
.......#...##......##...##.##..
|
||||
......#..##..#.#...#...#....##.
|
||||
....#.#..#.....#.##.#.....#.#..
|
||||
#..#.#.#........#...#.......##.
|
||||
##...........#..#........#.....
|
||||
....##....#....#.#.......#.....
|
||||
....##.#.#.....#.#.....#.....#.
|
||||
..........#.#..##..#..#.......#
|
||||
#....#.......##...#...#.....#..
|
||||
.........##.....#.#....#......#
|
||||
..........#........#..#..#.#...
|
||||
..#......#.....#......#......#.
|
||||
..#...###..##..#.....##..#..##.
|
||||
..#.#..###.........#.#...##.#.#
|
||||
#.........#..#......#...##.....
|
||||
...#...#.#..#...##.#...##.#..#.
|
||||
#.....#.....#.##....#.#......#.
|
||||
#....##..##..#.#..##....#.....#
|
||||
.#..........#..#...#..#.......#
|
||||
#.#.....#..##..##..#.#.........
|
||||
....#..##...#.....#.....#.#.#.#
|
||||
...#.#....#........#...#.#.....
|
||||
.#............#.......#.##.#...
|
||||
..##.......#.#...#........##..#
|
||||
..................##.#...#.#..#
|
||||
.#.........#.......#.....#.....
|
||||
....##...##..#..........#......
|
||||
..#.##..#....#..#............#.
|
||||
....####...#.##....##.#....#.##
|
||||
#..#....#......##........##....
|
||||
..###...........##..#......#...
|
||||
#..#.......#........#.......#..
|
||||
.....#....#..#..##.....#.......
|
||||
.###.####.#....#....#..#.......
|
||||
.............#...............#.
|
||||
.#..........#.#....#..#.#......
|
||||
..............##....#..#....##.
|
||||
.......#.#..#........#.......##
|
||||
#..#...#..#.#........#..#....#.
|
||||
...#.........#...#..#..........
|
||||
...#....##...#..#..........#...
|
||||
.#......#......##..##...#.#....
|
||||
.#.........#..###..............
|
||||
.................#.#.....##....
|
||||
...#............#..............
|
||||
#..#................#.......#..
|
||||
...#.......#......#.#.#........
|
||||
#.....#.##....#.....#........#.
|
||||
......##.#....#........#....#..
|
||||
.#..#.##...##........#.#.....#.
|
||||
..#...#....#...#..#..##..#.#..#
|
||||
#.................#.#.......##.
|
||||
..........#........#.#.....#..#
|
||||
#....##....#........##..##.#...
|
||||
#...#....#.....#.....#.....#...
|
||||
#..#..........#....##....#....#
|
||||
..#.#..#..#....#.#....#....#..#
|
||||
#....#..#.......#..........#...
|
||||
.#...#.#...#..#...#.......#....
|
||||
###........#......##..#...##...
|
||||
...#..........##..............#
|
||||
.......#........##......#.....#
|
||||
.#..........#...#...##....###.#
|
||||
.#...#....#..#.....##...#..##..
|
||||
.#.#.#...##..........##...#...#
|
||||
.#.....#...#........#........##
|
||||
#.......#......##.#.#..#....#..
|
||||
##..#.##........#....#..#......
|
||||
...#.......................#...
|
||||
..#....#..##........##.#.##.#..
|
||||
.............#.......#....#.#.#
|
||||
...#...........##..#.....#.....
|
||||
..#....##....#.....#...........
|
||||
..#.....#......#..#.###.##....#
|
||||
.#.......#...........#...#....#
|
||||
#............##...#...#.....#..
|
||||
##...#.....#.........##...##...
|
||||
...#...........#....##.........
|
||||
#.##..#..........##..#......#..
|
||||
.......#.#.......##.......#....
|
||||
..#.....##..#...#.......#......
|
||||
.#........#....##...........#..
|
||||
#.......#...#.#.###...#....#...
|
||||
..........##..#..#..##........#
|
||||
#....#....#...#....#....#......
|
||||
...........#....#...#...##.#...
|
||||
.........#.#.....#.............
|
||||
..####...........##..........#.
|
||||
.....#...................#.....
|
||||
#..##...#........#.###.#.##....
|
||||
....##...#.##................#.
|
||||
.#........###.#............#.#.
|
||||
..............#.##.........#...
|
||||
##............#.#..###....#...#
|
||||
#.....#........####....#....##.
|
||||
....##..#...##..##...##.....#..
|
||||
##..#....#.##.....####.....#.##
|
||||
##..#....#.##.##.#.#........#..
|
||||
....#..........##.....#..#..#..
|
||||
...#.......#........#.........#
|
||||
#..##.######.......##........#.
|
||||
###...#...####.......#.....#...
|
||||
#......#..#.....#......#.....#.
|
||||
..................##...#.......
|
||||
....#.#....#......#...#.....##.
|
||||
..#..#..#..#..#....#.#...#....#
|
||||
......#....###.................
|
||||
#.##......#...#......#.........
|
||||
#..#.#...##..#.......#..#...#..
|
||||
.#....#.#........#.........#...
|
||||
#.......##..#..#...............
|
||||
........#..##....#.....#..#....
|
||||
....#......##..#....#...#..#...
|
||||
#.....#...##..#...#......#.....
|
||||
.....#.....#.........##...#..#.
|
||||
........#...##.#...#.#....#..##
|
||||
....#....#...#.....##..#...#...
|
||||
#....#..#.........#.........###
|
||||
..###.....##...#.#....##......#
|
||||
#..#.#..#.......#..#....##.....
|
||||
###...#.##..#.......#......#...
|
||||
.....#.....##.......#...##..#..
|
||||
......#.......#.#.#......#..#..
|
||||
.................##..#.###.....
|
||||
..........#....#...#..........#
|
||||
...#.#...#.#..##.....#.#.##..#.
|
||||
.......#..#....#...#......###..
|
||||
...##..........#..#.....#....#.
|
||||
.#..##..###...#....##.....#....
|
||||
..#.#..............#....#...#..
|
||||
.....####.......#.#.##....#....
|
||||
#.#.#..##.##.#..#.##.#....#..#.
|
||||
........#....#.......##........
|
||||
...#...#....#...###.....###....
|
||||
.....#..#..........##.#...##.##
|
||||
..#.#.#..#....#...#..##...#...#
|
||||
..#......#..#.#.....#....#....#
|
||||
.#.....#.......#............#..
|
||||
#..##....#...#....##....#......
|
||||
#..#.........#...#...###.#..#..
|
||||
..#.#.#..#.#..#.......##.......
|
||||
...##...............#..#...#.#.
|
||||
.......####.#.....#..#..#......
|
||||
......#..#.....#..##....#......
|
||||
....#...#.........##.......#.#.
|
||||
#.#.#...#.....#...#..#.#..#....
|
||||
........#..#.........#..#..##..
|
||||
........###.#............#.#...
|
||||
#..#.......#.#..#.......#...#.#
|
||||
..##..............#.#.....#...#
|
||||
..##...........................
|
||||
..#.....#.......#......##......
|
||||
#...#......##.#....#.#.#...##.#
|
||||
#...#.#......#.#..##.........#.
|
||||
.##..#...#.#.....#.#.#...#.#..#
|
||||
.#..#...#.#.........#......#...
|
||||
...........#...#...#...#..#.#..
|
||||
.#........#...#......##...#.###
|
||||
#........#..#.#..#...........##
|
||||
.#...#...####.......#..........
|
||||
......#...............#........
|
||||
.....#.#.....#.#...#......#....
|
||||
.#........#...........#..##.#..
|
||||
....#..#.....###.......#...#...
|
||||
#.#.........#...##..#.#.##.#...
|
||||
................##.#....#.#...#
|
||||
.......#.......#......#...#....
|
||||
#....#.#..............#.##..###
|
||||
..##.##..#.....#............#..
|
||||
#....#..##........#....#.......
|
||||
.#.#........#.#................
|
||||
......##..#..#..........#..#.#.
|
||||
.....##.#..#....##.#......##...
|
||||
........###.#................#.
|
||||
#..###.....#.###.#...#.#.......
|
||||
.#..#.#.#.#..#..#.#.....#.#....
|
||||
#....#.....#..#......##...#..##
|
||||
........#...##..#.#.....#....#.
|
||||
.......#..#..#..#....#.....##..
|
||||
....#..##..#...#....#.........#
|
||||
#.#....#..#.#...#.#...#....#...
|
||||
.....#......###.......#..##.#.#
|
||||
.......##.....#....#........#.#
|
||||
.##.##..#..###.#....#.#.....#..
|
||||
..##.#.......###.........#.....
|
||||
.#...#......#..#....#..........
|
||||
.....#........#.....##...#.....
|
||||
..#......#.#.#..#.#....##.#...#
|
||||
#.#...#...........##......#....
|
||||
.................##.....#.#.##.
|
||||
###..#....#..................#.
|
||||
##..#.#.#...#....###.#.#...##.#
|
||||
#.#.#..#....#..............#...
|
||||
.....#....#......#..#.##.......
|
||||
#...#...#..###.......#.......#.
|
||||
.....#.#........#..#...#.#.....
|
||||
.....#..........#.###.......#..
|
||||
...#.##.....#....###.....#.....
|
||||
####........#....#..#.#.##.#...
|
||||
#......#...##.....#.#..##.#.#.#
|
||||
.....##....#..#.........##.....
|
||||
..##....##................##..#
|
||||
#.....#...##...##.#.....#...#..
|
||||
..#..#.#.#....#.#.......#......
|
||||
##.....##......#...#.........#.
|
||||
#..........#........#.#......#.
|
||||
.#..#.......#.#.....#..........
|
||||
.........#.#.......#.#..#..#..#
|
||||
#......#....#....#..##..##...##
|
||||
.....#..#...#.......#.....##...
|
||||
..#.##........#.###...#...#...#
|
||||
..#..#...........#..........#..
|
||||
.#.#.#...#.##.#..............#.
|
||||
....#..##.......#.....#..##..#.
|
||||
.#.##.#....##........#...##.##.
|
||||
...#.#...#....#....#......#####
|
||||
.....#.....##...........#......
|
||||
#........#.##.......#.#.......#
|
||||
#...#.......##.#.......#..#.#..
|
||||
#...##..#....#............#.#..
|
||||
........#.#..#...#..#...##..##.
|
||||
#...#....#............#........
|
||||
#.#.#.#.#....##.....##.........
|
||||
......##.........#.......#.#..#
|
||||
...#.#....#........#...........
|
||||
...#.#.......#.....#...........
|
||||
##....####......##.##..#.......
|
||||
#......#...#..#.#..#......#..#.
|
||||
#......#.#....#....#..#........
|
||||
..#.###...#.....#........#.#...
|
||||
..#.....##.....###....#.....#..
|
||||
#.##.#.....##....#...###.......
|
||||
###.#....###.#..##.#.......##.#
|
||||
#..#..##...#.#..........##.##..
|
||||
.......####.#..#.....##..###...
|
||||
#...#...##..#..##.......###....
|
||||
#....#.........##..#.........#.
|
||||
.....#.#..........#..#...#.#..#
|
||||
..........#......##..#..#.#....
|
||||
.#...#...#...#........###....##
|
||||
#....#.##..........#.#.....#.#.
|
||||
#....##.#.##..#.......#.#.....#
|
||||
.##..##..#.#...#.#...........#.
|
||||
....##..#...#.#.##.#.#...#.....
|
||||
.#...#.##........#.##..#.#....#
|
||||
.#.....##.........#.....#......
|
||||
..#.....#.#..#.##.............#
|
||||
##....##...#....##........#....
|
||||
.#....#........#.#..#..#..#.##.
|
||||
.#........#............#.......
|
||||
.#..##..##..#..#..####....#....
|
||||
..#.###....#..#.##......#.#...#
|
||||
.###..#.#...##....##....#..##.#
|
||||
....##........#....#.#.#...##..
|
||||
...#..#....#.#....#...#.#.....#
|
||||
...##....##..#....#.........#..
|
||||
.....#..##.###..#.....####.....
|
||||
...#..#.........#....#.#.##..#.
|
||||
...#..#...............#..#....#
|
||||
...........#.....#...####..##.#
|
||||
..#......#...#....#..#...##.#..
|
||||
.....#..#...........#.......#.#
|
||||
##....###...#.........#....#...
|
||||
...#..##.......#.#.....##....#.
|
||||
#.#...#.#....#.....#...##.....#
|
||||
.#...##....#.....#..##.......#.
|
||||
...#........##..........#.....#
|
||||
#...##..#.#....###...#..#......
|
||||
............#.......#......#.#.
|
||||
......#....#.#...#...#..#......
|
||||
.#..#......#....#.......#....##
|
||||
...#...#.......###..###...#....
|
||||
.............#.#...#..###.....#
|
||||
.#.....#........#...##....#..#.
|
||||
.....#.......#######.#.#...#...
|
11
2020/day03_toboggan_trajectory/tests/sample_input
Normal file
11
2020/day03_toboggan_trajectory/tests/sample_input
Normal file
|
@ -0,0 +1,11 @@
|
|||
..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
.#...##..#.
|
||||
..#.##.....
|
||||
.#.#.#....#
|
||||
.#........#
|
||||
#.##...#...
|
||||
#...##....#
|
||||
.#..#...#.#
|
Loading…
Add table
Add a link
Reference in a new issue