diff --git a/inputs/03-01.txt b/inputs/03-01.txt new file mode 100644 index 0000000..e3f8307 --- /dev/null +++ b/inputs/03-01.txt @@ -0,0 +1,323 @@ +......#..........##......#.#### +.##...###....#.....#...#.#..... +#..##..#.....#............#.#.# +##.#....#####..#....#..#.#..... +..#.#...##.##.......#.#..#..##. +##.#.......#.#.#..#...#.#...#.. +...#...#..#.##....##..#.#...... +.......##.#.#.#.##...#......... +..#...##.##...##..##.##...#.... +.#.#...#.....####..#.#...#.##.. +.#...#......##......##....#.... +..#.....#.........##.#...#.#... +...#.#...#..##...#....#.....##. +..#.....#..#..#......###....... +...##.#....##..##...........#.. +....#......#..#....###...#..... +.....#...#.#.....#..##........# +....#...#....##.#.##.#...#..#.# +.......##.#......##....#....#.. +...#.#...##..#...#..#..#..##.#. +##.#...#..#..................## +##........#....##...#..#..#.... +.#.#..............#######.##... +##..#..#.#.##..#............... +..#........#..#...##.......#... +............##.##.#..........## +.....##..#.....##..#.....#..... +..#.##.###.#..##.............#. +.........##...........#.....#.. +..#....#.#.###.#.#.......##.... +..###..##..#.#.##......#.#.##.. +##......#.#....##.#..#.#..#.#.# +..##.#.###.#...#............... +..####.......#...#.##....#..... +..#....##...#.#.#.#....#.##..## +.#...####..###....#.###...##... +..#.#..........#.#..#..#.....## +.#....#.........###...#.....##. +..#.#.#.##........#.##.#.....#. +#....#....###...#..#.........#. +#..#.###....#..............#... +............#....##.#......#.#. +...#..#.####...............##.. +....##......#.#.........####..# +.#....###..#.#..##........##... +#..##.....###..#............... +..#...........#........#...#..# +......................#.#..#... +.#.##.#..#.#....#...#...#.#.... +..#..#.........#..#.#.......... +.#......#####...#......#..#.... +..........#.....#..#.##.####.## +##.##..#............#####...#.# +..##..#..###......#...#...#.... +....#####........#.##...###.... +......#...##..#..#............# +...#....##.##...#..#...#....... +....#####.#...............###.. +.#....#..##....#.#.#..##.##...# +...#..#..#........#.#####.....# +......##.#...#..#..#.....#..### +###.......#.#........#......#.# +..#.#..#..#........#..#......#. +...##.........#..........#..... +...#..###.#.......#.#.........# +....#..#.##...##.....#.....##.. +#.#.#.#.....##.##.###..#.#....# +..#....#.....##.####..#........ +...#..#.##.....##.#..#....###.. +.#..#.....#....#...#.#.......## +..#..#.......#.#.###......#.##. +.###.####....##............##.# +#....###.#......##.#......##.#. +.##...........#.#....#......... +#.##..##...#...........###....# +#.#..#...#.#..#..###.#.##...#.. +..#...#.#..##....#..#..#....... +#..##..#.####...#...#..####.##. +###..#.##....#...#.###..##...## +##..#..#.#....#.....##.......#. +..#..##.##.#.......###.#.....#. +..........#.####....#.......#.. +#...#.#..#.......##......##..## +##...##.##..###...............# +....##.#...#.......##...##..#.. +.#.........#...#.#...##.#.....# +.#...#.#..#...#..##....#..#...# +.#.#...#..#..###...##....#..... +.........#.#...####..#...#.#... +...#.............#.#..........# +...#...#..##.#........#.#...... +...#...#.....#....#..###.##.### +.#.#........#....#...#.###.#.#. +##.....#.......#..##.#....#..## +...###...#.#.#.#....#.#....#... +#...#.#.......##.#..#....#.#... +#...#......###.....###........# +..#.##...##....#...#....#.#.... +#....#..###....##.#......##...# +##.#...#..........#.##....#..#. +.##....#............###.#...#.. +###.##.#####.##.....##..#####.. +..###.###.......#.#...#....#... +.#...#....####.........#....... +..##.#.#......#....#.#....#.#.# +#.####.....#....#..#.....#.##.. +###.###.##...##.#.#.#.....#.#.. +.......#.....#.......##.#.....# +#..#.##...#........#.#.......## +#.#........#...#....#.......... +..#....##.#......#..#.......... +#....##.....#.....#.##.#...#... +....#.#.....#....####...#.#.##. +......#.......##...##.#......#. +.#.........##...#..#..##..#.... +.#...##.....##.#....#.......... +....#.###..##..#...#..........# +......#...#.#.#........##...... +.#..........#.#.....#..##..#.#. +.......###.#......#....#.#..#.. +..##.......#....#....#.#...##.# +#.##.#.......#..###..##...#.#.. +......####....#.#.....#...#..#. +#.##.###..#..#.#.....###..#.#.# +#.#.#..#.#..##...#...#..##.###. +....##..##.#...............#.#. +..###.#.#.##..#....##.......#.. +#.#....#..........##......##### +.#.#.......##.#.#......##..#.#. +......#.###.##.#..#....#.##.... +..###........#.......##.#.#.... +.#..##.............#.##.###...# +.#####...#......#.......##..... +##..###.#...#....#..#....#.#..# +.#.........###.##.....##.....## +.##.#....#..#.#..##..#....##... +.#..#..#......###...#.......#.. +#.#...#.....#..#.#.#..#..###... +....#....#..#..#....#..#.#.#... +......#.......#.#.#.#.....#.... +###...#...#......#..#.#.#..#.#. +#...##.##.##........##....#.... +.....#.......#...#...#.#.#....# +...##.....##.#...#.#.#.#..#..#. +.#.......##...........#...#.##. +.##..........#......#.#...###.. +.....##...#.....#...#......#... +...........#.....#..#...#..#.#. +#.....##..#...........##....#.. +#.##...###.###....##..#..#....# +#.#.##...##....###....##.##.... +.#..###.....#......#...#...#..# +..#...#....#.#.###.#..#......#. +......#.........#..#.##...#...# +..#.#....##.#..##..##...#....#. +#.....#....##.........##.#..... +...#...#..###.###......##...### +.##.###...##..#.##....##.#..#.. +..#..#.......#................# +.....#..#.#.#..........##..#... +......###.#.#............#..#.# +..#.##.....##....#...#...#.#... +..#......##...#...##........#.. +#.....#.....#..#......#.###...# +....#..#.#.....#...#....#.#...# +#.......#..#...##..#.#..#.##... +..#......###...#.........##...# +...#.......##.....#..##........ +.#....#.#.....##.#.#........... +##..#..#...#.##.#.#.#.#.#..##.# +##...####.#.#.##...#..#......#. +#.##..####.##.#.........#...### +#...#.......#.#..####.#.#.#.... +#....#........#........#....... +..#..####.....#....##...###.##. +...#.#..####.........#....#.##. +##.#...#...#..#.#..##.....##... +....#.........#.##........##.#. +##...#......#....#..#....#....# +###.....#......##...#...##...#. +#.##...............#.......#... +.##.#...#..#....#.#.....###..#. +.....##...#.##.....##...#....#. +#.#..#..........#####..##...... +..#.........##...#.........#.## +...#..##.#.#..#......#..###.### +#..#...#.#...##..........#..... +.###..#....###.....#....#..###. +#..#....#...#........##.....#.. +.#..###........#....#..####..## +.#..#.#.#.......##.#..##.#..##. +..#..###......##....#..#..#..#. +.......###..##....#......#...## +#........#.##.............##.#. +...#.#.#....##....##.###...#... +..#.....#..##..#.#.......#.#### +.#......##.........##...#.....# +.#.###........##....###.#.#...# +##...#.#....#.....##.......#..# +#...........#...........####... +#..#.#..##..#...#....#.##....#. +................##............. +..##...#.#....##....#...#...... +.#.....#....#....#..#..#.#..##. +.....######.#.#.##.###.#....... +..#####....#..#...........#.#.. +.......#..#..##.#...###.#.#.### +###...#...#..##.#.##..#...#..#. +.#..#..............#........... +.#.....#.....##....#....##..#.. +....#####.#....#......#.......# +.#.#.....##.####..#...#.......# +.#...##.#.......#.....##.#..##. +..........#...#....###....#...# +..#......#...#...#..#.#........ +.......#.......#..####..##..... +.#..#.....###...#...#...#...#.. +##..#.......#.#...#..#..#.##..# +#..#...#.#.....#.##.#........#. +......#......#.#..###.##..###.. +.#..#..#.##.#...........#...##. +.#....#...#.#..#.#.#...##.#..#. +##.#....#..#..#.#...#......#.#. +..#.#............##...#........ +...####...#...#.....##..#...### +....###.......###.##..#.###.... +#......#.#....#.#.##.#.##..###. +.....##.....#..##.....##....#.. +..#...#..##.#.##.#.#.#.......## +#....#..##.......#......#..#.## +#.....##...#..##......##.#.#..# +....#..##..#.##...#.#.##..#..## +#..#...##....##..#...#....#...# +.##.#.#....#.....#........##.#. +..##..#....#........#.....#.... +.##.#..##...#.....#...###.....# +#..#..#........#..#.....#.#.#.# +..##..###.#..#...#.#......#..#. +#.....#.....#.###......##..#.#. +.........#...##.........#...#.. +.##.#.##......#.#...###..#....# +...##.#..###........##......#.. +...#.#...#......#.#.#....#..#.. +..####.........#..#....#....... +#..#.........##.#.##....#.....# +..#..#..#.#........#.###....... +##.#..#..#....#...##.......#..# +..#.#.....#.............#...##. +..........#...##.....#..#.#..#. +....#..#...#..##..#...##.#..... +##....#......#..#.....#..#..... +...#.#.#.#...........##...#.#.. +....#.###...#............#..... +.#.#.#.......#.#......#....#.#. +#.#.#.#..##.#..#..##...##.#..#. +.#.##....##..#........#....#... +####...#....#.#...#..#..###...# +.....#.#.##.......##..#.######. +.......#.#.#.....#.#..##....#.. +..#....#.#..#.#.#..#..#........ +.....##......#.........#.#...## +#....##.#.....#..........#.#... +#...#.#..#.#..#.#....#..#.#.... +....##........#................ +###.#.#...#..##...#...#.##...#. +...#....###..#..##..#..#....... +.....#..........#.#........##.# +.#........#.##.#..##..#...#...# +..##....#...#.#.........##.#... +......#...#......#.....#....... +....##.##..#.##...#.#.#.##.#.#. +..#...#.....#.#....##.#........ +.#.#.......#.......###..#..#... +#...#..#..#..##....#...#.....#. +.#..####.##.....##.........#.#. +#...###.......#...####..##..... +#.##.#....#.#.##.......#...#... +..#.......#.#.##.##..#...##.... +.#.......#.#..#.....#.....#.#.. +..#..#.......##.....#.#.....#.# +#...###..#..#..##...#.....#..## +......#................#....... +..#.....##..#.......#...#...##. +...##...####.#..#...#.......##. +..#...#..#...#...#..#..#####... +#..#...#....#....#...........#. +..#.......#..#.##...##..###...# +.#..#..#......##...#....#...... +...#..##....#..........#.....#. +###...#.#......#.#.....#.....## +#.#..#.....#........#.##.#.##.. +....#...#.....#..#.......#.#... +#.#...##....#..#.....#...#.#.#. +.#......#...##..#.......#...... +...#...#.#.#.###.#..#.#..#..... +###...#..###.#...#..##...####.. +.#.#.#..#........#..#......#..# +.#..#....#......#....#.#...#... +.##..........###...##.....#.#.. +.#...#.#.##.##..###.#...#..###. +......#......#......#.##......# +..#.##..#.#..#....##..##...#... +.#......#..#...##....#...#..... +.#.....#.##..........#..#...... +###.#..#.##..#..##...#..#...#.. +#.....###........#.#..##.#..... +.....#.......##.....##.....#.## +...##.#......####....##........ +..#..#..#....#.##.....##.####.. +...#..#....#.#..#.#..#.#.#..#.. +#..........#....#.#.#.#...#..#. +...####.##...#..#.......#.#..## +#........#..#.................. +.#..#....#.#.#..#..........#... +###...#....####....#......#..#. +#.........####..#..#........... +.....##..#..##.##.##.#..#.....# +.#..#.#.##..#..#.#.#.##.###.... +......##......#...#.##....#..#. +.#.#....#..#......#..#...###... +.##...#......##...###...#.#...# +.......#.#....#............#..# +.#..##.#.######...#...#......#. diff --git a/src/aoc03/mod.rs b/src/aoc03/mod.rs new file mode 100644 index 0000000..3c1ae42 --- /dev/null +++ b/src/aoc03/mod.rs @@ -0,0 +1,34 @@ +use std::error::Error; +use std::fs; + + +pub fn both(filename: &str) -> Result<(), Box> { + 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 +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a014f60..c25537a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod aoc01; -pub mod aoc02; \ No newline at end of file +pub mod aoc02; +pub mod aoc03; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 20c9782..e3ad81f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()), };