Compare commits

...

1 Commits

Author SHA1 Message Date
Dan Ballard 3ae45e3cf4 day 3 2020-12-06 18:42:15 -08:00
4 changed files with 361 additions and 1 deletions

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

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

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

@ -0,0 +1,35 @@
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()),
};