Completed day 3
This commit is contained in:
parent
e4c3c74dd9
commit
aa5f86ecff
14
day03/Cargo.lock
generated
Normal file
14
day03/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7"
|
||||
|
||||
[[package]]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
]
|
10
day03/Cargo.toml
Normal file
10
day03/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
323
day03/input/input.txt
Executable file
323
day03/input/input.txt
Executable file
@ -0,0 +1,323 @@
|
||||
.#.#....##.......#..........#..
|
||||
...#...........##...#..#.......
|
||||
#.####......##.#...#......#.#..
|
||||
##.....#.#.#..#.#............#.
|
||||
##.....#....#.........#...##...
|
||||
###..#.....#....#..............
|
||||
..........#..#.#..#.#....#.....
|
||||
##.....#....#.#...#.##.........
|
||||
#...#......#....##....#..#.#...
|
||||
.##.##...#....##..#.#.....#...#
|
||||
.....#.#..........##.#........#
|
||||
.##..................#..#..##.#
|
||||
#.#..........##....#.####......
|
||||
.#......#.#......#.........#...
|
||||
#....#..##.##..##........#.#...
|
||||
##..#.##..#...#..####.#..#.....
|
||||
###....#.###.##...........##..#
|
||||
.....#.##.....##.#..#####....##
|
||||
....#.###....#..##....##...#...
|
||||
..###.#...##.....#.##..#..#.#..
|
||||
#...#..#..#.........#..#.......
|
||||
##..#.#.....#.#.#.......#...#.#
|
||||
...#...##.#........#...#.......
|
||||
..#..#.#..#...#...#...........#
|
||||
........#.....#......#...##....
|
||||
#........##.##.#.#...#...#.....
|
||||
####.......#.##.###.#....#.....
|
||||
...#...........#...#......#...#
|
||||
##...#...#............#.......#
|
||||
....#...........##.......#.....
|
||||
###......#.....#....#...#.#...#
|
||||
.....##..........#.......#.#...
|
||||
##.##.##...#......#....#.......
|
||||
##..#.#..#......#...#..#.......
|
||||
....#....##.##............####.
|
||||
..#.###..#.##.###..#.##.......#
|
||||
#.##..#.#.....#..#.....##......
|
||||
..##..#.....##.#.##........#...
|
||||
.#..#.#......#..#............#.
|
||||
.....#..#.#...#....#.##.#......
|
||||
.#...##.#..#.#...##...##..##...
|
||||
###............#.#..#..#...#...
|
||||
..#..##.####.#.....#.....##.###
|
||||
#....#.##..##....#..#...#.##.#.
|
||||
.....#.##.........##...##......
|
||||
.........####.#....#.#......#.#
|
||||
.........#.#..#...#.#..#.#....#
|
||||
.#.....#..##.##..##....#.......
|
||||
..........##......#.##.###....#
|
||||
.##...###..##.#...#........##..
|
||||
..............#.#....#.#.###.##
|
||||
..##.##.......#.#......##...#..
|
||||
.#.....#..##..#.###...#..#.##.#
|
||||
#.....#.#..#...#........#...#..
|
||||
.#......#....#.#.....###...#..#
|
||||
..##.#....#..##......#.....#...
|
||||
..#.#.##..#.....#.####..###....
|
||||
.........#......#..#...........
|
||||
..#........#.##.#.....##.##..#.
|
||||
.......#.........#....#...#.#..
|
||||
.##.....#.#....#.#.......#.....
|
||||
..........#.##........##...##..
|
||||
###..###.#.#..#..#####.##.#.##.
|
||||
..##..##.#.#...#..#.#.#......#.
|
||||
#..#..#..#..##..#.....#......#.
|
||||
..#....#.##..#......##.........
|
||||
..#.##......#...##.#......#....
|
||||
.......#..#.##.#.....#.........
|
||||
.......#.#.#.###...##......#...
|
||||
.....#.#..........#..#...#.....
|
||||
....##..........#..........##..
|
||||
..#......#.....#.##.#..#...#.#.
|
||||
....#.....#..#...#..#.#.##..###
|
||||
.####....#........#...#........
|
||||
...##.#.##.#..#...##...#.##....
|
||||
....#...#...#.#.#.#...#..#.....
|
||||
.....#...#.#.....#.#........##.
|
||||
..#.#.......###.#.....##.......
|
||||
......#.........##....#....#..#
|
||||
.............##.....##.........
|
||||
.........##...##.......#.....#.
|
||||
##.........#..........#.###..##
|
||||
...#.....#......#....#..##.....
|
||||
##..#...#...##.#.....#.#......#
|
||||
..#...##.#.......#.#......#.##.
|
||||
......#.......#.#...........#..
|
||||
..........#.....##............#
|
||||
#........#...#..#.......###.##.
|
||||
.##...........#.#........#.#.#.
|
||||
...#..##...#.#....#####.#......
|
||||
.....##...###...#..#.##...####.
|
||||
...#....#.....#..#.......#.....
|
||||
#....#....#...#..#..#.######..#
|
||||
#.###...........#......#...#..#
|
||||
.#.#.#.#..#....#....#...##.#...
|
||||
.#..#.........#.#....###...#...
|
||||
......#..##.##..........#....##
|
||||
.....#......##....##.....#...#.
|
||||
.#...#.#.#....##....#..#....#.#
|
||||
..................#..###.#..##.
|
||||
..#.........#......#....#..###.
|
||||
#.#.....#..#..#....###..###....
|
||||
..##..##.#..##........##...##..
|
||||
##..#........##..###..#.....#.#
|
||||
..#..###..#......#....#...#...#
|
||||
#..#.#..............##.#..#.#..
|
||||
.....####....#...####.....#.#..
|
||||
.....#....##.#......###........
|
||||
##.##...#.#.#.#.......#....##..
|
||||
.#......#...#.#....#..##.#.##.#
|
||||
#.#.##.#.#......#..##........##
|
||||
...##.....#.....#...#..###...#.
|
||||
........###.....#.....#...##..#
|
||||
.....#.##.##......#.#....#...#.
|
||||
.#....##.......#..#.####.......
|
||||
.#..#....#..........#......#.#.
|
||||
.#.##.##.....###.#.#...........
|
||||
.........#......#..##..........
|
||||
....#...##.#.#.#..#.#.........#
|
||||
..#.....#.##...#..#..#.###....#
|
||||
...#.##......#.....##....#.....
|
||||
###............#.#....#...#....
|
||||
.......#.....#..#.#.#....#..#.#
|
||||
...#......#.#..##..#....#...#.#
|
||||
............##........##..##...
|
||||
..#..#.##..#......###..#.......
|
||||
........#.........#............
|
||||
..#...#.#########.#...##..###..
|
||||
#....#......#.......#.#.....#..
|
||||
#.#..#....###.###....#...#.#...
|
||||
#...###.#.#.......#.##......#..
|
||||
.................#...#.#.#.....
|
||||
##....#...#........#....#.#..#.
|
||||
......#.....#...#..........#.#.
|
||||
##..........#...#..........#.##
|
||||
..#.#.##.#....#.#......#...##..
|
||||
.....#.......#..#.....#........
|
||||
#.##.#..##..#.......##.........
|
||||
....#......#..#..#.#...#.......
|
||||
...#....#................###...
|
||||
.##.....#.#....#.#..........##.
|
||||
...#..#....#.##.##......#......
|
||||
..#.#....#.......#.#..##.......
|
||||
....#.....#..........##.#.#####
|
||||
#.....................##..#..#.
|
||||
.###..#.##.......##.#...#..#...
|
||||
...###.......#..#...#......#..#
|
||||
#..#...#.#..#.#..#..#.##.......
|
||||
#...##.......#..#..#.##..###...
|
||||
......#....#.#.#........#.##..#
|
||||
..##..#....#....#..#.#..#......
|
||||
..##.#...#.#######..#...#.....#
|
||||
..#....#..#.........#..##......
|
||||
...#....#.#......#..#..#.#.....
|
||||
#..#....#........#.#..##....###
|
||||
#....#..##......##.##.....#.###
|
||||
...#.#..........#..#.#.#.#.##..
|
||||
......##..#.#..#.#....#....#...
|
||||
##....#....#..#..#.##......#...
|
||||
....#.#..##.#.#...###....##.#..
|
||||
...#.......##..#.......#...#...
|
||||
......##.......#..##.....#...#.
|
||||
...#.#...#...........#...#.....
|
||||
.#....#...#......##.##..###..#.
|
||||
.#..........#...#...#...##.##..
|
||||
.....###..#.....#..##....#.####
|
||||
..#.###..#..##..##.....#.#.....
|
||||
.............#.###...##.#.....#
|
||||
....###.......###.#.....#..#.#.
|
||||
........##.#.........#.....###.
|
||||
.....###.#..#.....#...#..#.....
|
||||
.#....#..##.#..#.#....#.......#
|
||||
........#......#.#..#.#..#...##
|
||||
...#.##.##......#..............
|
||||
.#.....##.#.....#..#......##...
|
||||
#..#..#.....#.....#.....###....
|
||||
.##...........#..#.##.....#....
|
||||
..#.#......#.#...#.##.#..#...##
|
||||
...#..........#.....#..........
|
||||
#.#.#.#.#...#....#...#.....##..
|
||||
#......##...#...#..........#.#.
|
||||
....##........#.#..............
|
||||
#..#.#.#..#........##......#.##
|
||||
........####...##.#.....#......
|
||||
....#........#.#..#..##..#.#...
|
||||
.#.....#..###...#..#.....#..#..
|
||||
#......###.#..#....#..#.#......
|
||||
....#.....##.##..#...#.#..##.#.
|
||||
..##..#...#.#......#....#...#.#
|
||||
#..##...##..#...###...#..#.....
|
||||
.......#.....#...........##....
|
||||
#..##....#........#....##..#.#.
|
||||
.#........#..##...###.#..#.....
|
||||
.#.#....#..##...#...##.#..###..
|
||||
#.........#.......#.....#.#....
|
||||
#..#.....#.#.###.#..#......#...
|
||||
....#..#.#....#..##..###....###
|
||||
###.##.#.#..#...........#.#.#..
|
||||
..##.#.......#......#..##....#.
|
||||
.....#.#.#.......##.......#...#
|
||||
...........#.##....##.##....#.#
|
||||
...#.......#..#.##..#......#..#
|
||||
#.#.#...#......##.#...........#
|
||||
##........#...........###.#..#.
|
||||
..........#.#.#....#.#..##.#.#.
|
||||
...#.#.#....#..........#..#....
|
||||
#.#....###.#.#..#.......###...#
|
||||
.#....#......#.#.#..#..#.......
|
||||
......##.............#....#.#.#
|
||||
.#..........#.........#.##.....
|
||||
##....#....##....#..#.......#..
|
||||
#.##.##.#..#..#.....#..#.##.#..
|
||||
.#..#.......##..#.....##.##....
|
||||
.......#..........#.#.##..#.##.
|
||||
....#.....#.#...##....##.......
|
||||
.......#.........#...##....##.#
|
||||
#.....#......#..........#...#..
|
||||
...#.#.......#.#..#....###..#..
|
||||
.....#.#.#.........#...........
|
||||
.#..###.#.#........#.#.........
|
||||
.........#..#......##...##....#
|
||||
...###..#.....##.....#.###....#
|
||||
.##...#...#........###.#..#....
|
||||
.##........#..#.###.######.##.#
|
||||
##.#...#.#....#..##.#....##....
|
||||
.......##.....##.#..###.#......
|
||||
..##...##........#.......#....#
|
||||
#..##...#.####...###......#...#
|
||||
.##.....#.##.#.#.....###.#..##.
|
||||
..###....#.#.###.#....#........
|
||||
....#..###..#...#....#..#..#.#.
|
||||
#.#.##....##...##.......#......
|
||||
.........#...#....#..#.........
|
||||
.............#...#..##.#.......
|
||||
...#.##.......#...#.#..##.##...
|
||||
.####.#.##..#.#......#.##...#.#
|
||||
.#..#.#.....#.................#
|
||||
..#.##..###....#...#......####.
|
||||
..##..##...........#....#...#..
|
||||
....#...#...#...#.......#....#.
|
||||
#.#...###...#...#.#...#....##.#
|
||||
......#...#.#.......#.....#...#
|
||||
....##...#.#.#....#....#.#....#
|
||||
.....#.....#...##..#...#....##.
|
||||
#.....#....#......##.##....#...
|
||||
...#.#....#...#....#.#....##..#
|
||||
...#.#..#...##....###..#.......
|
||||
...##......###...###.#...#..#..
|
||||
##.......#.......###.......#..#
|
||||
..##.##..###.#............#...#
|
||||
#.....##..#..##....##..#.......
|
||||
......#.#...#......#.....#.....
|
||||
#...........#....#..##.##.#....
|
||||
.......#..#......#...#....#...#
|
||||
.#...##...........#......#...#.
|
||||
#........#....##...###.#....#..
|
||||
.....#.......##.........#.##...
|
||||
.#.###..#....#..##.#..#.#..#...
|
||||
#.......#.##.#.#....#.#..#....#
|
||||
###.....#.#.......#..#......#.#
|
||||
#..#.#.......#.#..##..##.#.#...
|
||||
#..#.#.#.###........#.....#...#
|
||||
#.#.#..#..##.....#...........#.
|
||||
..#.#..#.....#...#...#...##....
|
||||
...#.##......#...##.#...#.#.#.#
|
||||
#..#.#.#.#.......####..........
|
||||
..#......#.#......##.###.....##
|
||||
..#...##..#.........##....#.##.
|
||||
##.##.##.#.#.....#..........##.
|
||||
.#.....###.#..#....#..#.###...#
|
||||
#...##.......###....#.#..#.....
|
||||
..#....##.........##.........##
|
||||
......#....#.##.......#........
|
||||
..#.#.#..#...#...#...##.#...#..
|
||||
......#..##.#.#.#...##...#.#.##
|
||||
#..#...##.#.....#...#.##.......
|
||||
..#..#.........##.#...#.##...##
|
||||
##.##.#....#.......#.##..#.....
|
||||
.....##...##.##...##.........##
|
||||
#......#...#.......#...#...#...
|
||||
...##...........#...#..#.......
|
||||
.#.##.#..#........#....#.......
|
||||
#.#...#..#......##...#.#.##....
|
||||
##........####..#.#...#.#.##.##
|
||||
#..#.#.##......##.#.#..#.......
|
||||
.....#.........#..#.####....#..
|
||||
......##..#....#...#.#....#....
|
||||
#...##........#.........#.....#
|
||||
.#.#...#.#.#..#............##.#
|
||||
.#..#....#....#.....#...#.....#
|
||||
..###...#..#.....#.##.###...#.#
|
||||
.#.###..#..#...#.#...#.#......#
|
||||
#...#####......###........##...
|
||||
.....#.....#..#.#....#..##.....
|
||||
....##...#.#.##.#####...#....#.
|
||||
.#.#.........##.#.......#..##..
|
||||
.#...#.#...#...#....#.#...##.#.
|
||||
.##...#..#.#..#......#.#.#..##.
|
||||
..#.....#..#.....##.....#......
|
||||
..#........#..##...#.......###.
|
||||
.#....#.......#....#....#..#...
|
||||
....#......#.#.#.........#.....
|
||||
..##...#.#.#...#.#........#....
|
||||
.#.....####...##.#..#...##.....
|
||||
...#.....#...#...#....#....#...
|
||||
.........#..#.#.....#..#.#..#..
|
||||
.........##...........#.......#
|
||||
......#..#.....##...#.##.#.....
|
||||
.#......##........##...#.#.##..
|
||||
.....#.#..##...........#..#..#.
|
||||
...#.......#...#.#..#.##..#.##.
|
||||
...#.......#.....#.#...#.##.#..
|
||||
#.....#.............##.#..####.
|
||||
.#...#......#...##.#....#.#....
|
||||
.##..##.##....#.#.....#.......#
|
||||
...#...#....#....##.#..#....##.
|
||||
..............##....#.......#.#
|
||||
.#.#.#...##..#..#...###.#..#...
|
||||
.#.#...#.#..#.#..#...######..#.
|
||||
........#......#.#..#.#....#...
|
||||
..###.....###.#.##....#...##...
|
||||
.##.#.....#.......##.......#...
|
||||
..#..##...#..........#.#....#.#
|
11
day03/input/test.txt
Normal file
11
day03/input/test.txt
Normal file
@ -0,0 +1,11 @@
|
||||
..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
.#...##..#.
|
||||
..#.##.....
|
||||
.#.#.#....#
|
||||
.#........#
|
||||
#.##...#...
|
||||
#...##....#
|
||||
.#..#...#.#
|
158
day03/src/main.rs
Normal file
158
day03/src/main.rs
Normal file
@ -0,0 +1,158 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
|
||||
const INPUT: &str = "input/input.txt";
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
enum Cell {
|
||||
Tree,
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl From<char> for Cell {
|
||||
fn from(c: char) -> Self {
|
||||
match c {
|
||||
'#' => Self::Tree,
|
||||
_ => Self::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Cell {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Tree => write!(f, "#"),
|
||||
Self::Empty => write!(f, "."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Slope {
|
||||
pattern: Vec<Vec<Cell>>,
|
||||
}
|
||||
|
||||
impl fmt::Display for Slope {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for row in &self.pattern {
|
||||
writeln!(
|
||||
f,
|
||||
"{}",
|
||||
row.iter()
|
||||
.map(|cell| format!("{}", cell))
|
||||
.collect::<Vec<String>>()
|
||||
.join("")
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Position {
|
||||
x: usize,
|
||||
y: usize,
|
||||
}
|
||||
|
||||
impl Slope {
|
||||
fn get_cell(&self, position: &Position) -> Cell {
|
||||
let row = &self.pattern[position.y];
|
||||
row[position.x % row.len()]
|
||||
}
|
||||
|
||||
fn trees_in_traversal(&self, traversal: &Position) -> usize {
|
||||
let mut position = Position { x: 0, y: 0 };
|
||||
let mut tree_count = match self.get_cell(&position) {
|
||||
Cell::Tree => 1,
|
||||
_ => 0,
|
||||
};
|
||||
while position.y < self.pattern.len() - 1 {
|
||||
position.x += traversal.x;
|
||||
position.y += traversal.y;
|
||||
tree_count += match self.get_cell(&position) {
|
||||
Cell::Tree => 1,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
tree_count
|
||||
}
|
||||
}
|
||||
|
||||
fn solve_part1(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let slope = Slope {
|
||||
pattern: reader
|
||||
.lines()
|
||||
.map(|line| line.unwrap().chars().map(Cell::from).collect::<Vec<Cell>>())
|
||||
.collect(),
|
||||
};
|
||||
Ok(slope.trees_in_traversal(&Position { x: 3, y: 1 }))
|
||||
}
|
||||
|
||||
fn solve_part2(input_path: &str) -> Result<usize> {
|
||||
let file = File::open(input_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let slope = Slope {
|
||||
pattern: reader
|
||||
.lines()
|
||||
.map(|line| line.unwrap().chars().map(Cell::from).collect::<Vec<Cell>>())
|
||||
.collect(),
|
||||
};
|
||||
let traversals: Vec<Position> = vec![
|
||||
Position { x: 1, y: 1 },
|
||||
Position { x: 3, y: 1 },
|
||||
Position { x: 5, y: 1 },
|
||||
Position { x: 7, y: 1 },
|
||||
Position { x: 1, y: 2 },
|
||||
];
|
||||
Ok(traversals
|
||||
.iter()
|
||||
.map(|traversal| slope.trees_in_traversal(&traversal))
|
||||
.fold(1, |acc, count| acc * count))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Part 1: {}", solve_part1(INPUT).unwrap());
|
||||
println!("Part 2: {}", solve_part2(INPUT).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "input/test.txt";
|
||||
|
||||
#[test]
|
||||
fn parses_input() {
|
||||
let file = File::open(TEST_INPUT).unwrap();
|
||||
let reader = BufReader::new(file);
|
||||
let slope = Slope {
|
||||
pattern: reader
|
||||
.lines()
|
||||
.map(|line| line.unwrap().chars().map(Cell::from).collect::<Vec<Cell>>())
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let file = File::open(TEST_INPUT).unwrap();
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut buf = String::new();
|
||||
reader.read_to_string(&mut buf).unwrap();
|
||||
|
||||
assert_eq!(format!("{}", slope), buf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 336);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user