diff --git a/2020/src/day3.rs b/2020/src/day3.rs index 8dc76a4..2d7083e 100644 --- a/2020/src/day3.rs +++ b/2020/src/day3.rs @@ -49,6 +49,18 @@ //! 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? +//! +//! --- Part Two --- +//! Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all. +//! +//! Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom: +//! +//! Right 1, down 1. +//! Right 3, down 1. (This is the slope you already checked.) +//! Right 5, down 1. +//! Right 7, down 1. +//! Right 1, down 2. +//! In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied together, these produce the answer 336. use std::ops::Index; @@ -95,10 +107,19 @@ fn parse(input: &str) -> Bitmap { } #[aoc(day3, part1)] -fn answer(map: &Bitmap) -> usize { +fn answer_part1(map: &Bitmap) -> usize { (0..map.height).filter(|y| map[(*y * 3, *y)]).count() } +#[aoc(day3, part2)] +fn answer_part2(map: &Bitmap) -> usize { + (0..map.height).filter(|y| map[(*y, *y)]).count() + * (0..map.height).filter(|y| map[(*y * 3, *y)]).count() + * (0..map.height).filter(|y| map[(*y * 5, *y)]).count() + * (0..map.height).filter(|y| map[(*y * 7, *y)]).count() + * (0..map.height / 2).filter(|y| map[(*y, *y * 2)]).count() +} + #[cfg(test)] mod tests { use super::*; @@ -114,4 +135,15 @@ mod tests { } ); } + + const INPUT :&'static str="..##.......\n#...#...#..\n.#....#..#.\n..#.#...#.#\n.#...##..#.\n..#.##.....\n.#.#.#....#\n.#........#\n#.##...#...\n#...##....#\n.#..#...#.#\n"; + #[test] + fn part1() { + assert_eq!(answer_part1(&parse(INPUT)), 7); + } + + #[test] + fn part2() { + assert_eq!(answer_part2(&parse(INPUT)), 336); + } }