This commit is contained in:
Dan Ballard 2020-12-06 18:42:15 -08:00
parent 8d760a131d
commit 1f58f1ec51
4 changed files with 360 additions and 1 deletions

323
inputs/03-01.txt Normal file
View File

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

34
src/aoc03/mod.rs Normal file
View File

@ -0,0 +1,34 @@
use std::error::Error;
use std::fs;
pub fn both(filename: &str) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(filename)?;
let lines = contents.lines().collect();
let trees01 = calc_trees(&lines, 3, 1);
println!("Encountered {} trees while going down", trees01);
let trees02 = calc_trees(&lines, 1, 1) * trees01 * calc_trees(&lines, 5, 1) *
calc_trees(&lines, 7, 1) * calc_trees(&lines, 1, 2);
println!("Part 2: {}", trees02);
Ok(())
}
fn calc_trees(contents: &Vec<&str>, right: usize, down: usize) -> u64 {
let mut trees = 0;
for height in 0..contents.len() {
if height % down != 0 {
continue;
}
let x = height/down * right;
let row = contents[height].repeat( x / contents[height].len() + 1);
if row.as_bytes()[x] == "#".as_bytes()[0] {
trees += 1;
}
}
return trees
}

View File

@ -1,2 +1,3 @@
pub mod aoc01;
pub mod aoc02;
pub mod aoc03;

View File

@ -9,6 +9,7 @@ fn main() {
let result = match day {
1 => advent_of_code::aoc01::both("./inputs/01-01.txt"),
2 => advent_of_code::aoc02::both("./inputs/02-01.txt"),
3 => advent_of_code::aoc03::both("./inputs/03-01.txt"),
_ => Err(format!("unsupported day {}", day).into()),
};