diff --git a/2020/inputs/03.txt b/2020/inputs/03.txt new file mode 100644 index 0000000..26d1b5b --- /dev/null +++ b/2020/inputs/03.txt @@ -0,0 +1,323 @@ +..#......###....#...##..#.#.... +.#.#.....#.##.....###...##...## +..#.#..#...........#.#..#...... +..#......#..........###........ +...#..###..##.#..#.......##..## +......#.#.##...#...#....###.... +..........##.....##..##......#. +......#...........#............ +#....#..........#.............. +.#........##.............###.## +....#.........#.......#.#....## +#.#..#..#..#.......#...#....##. +.#........#......#.##.......#.. +..#.....#####.....#....#..#..## +.......#..##.......#......#.### +..#.#...#......#.##...#........ +##...................#...##..#. +......#...#.##...##.#......#..# +.#.................#..##...#... +...#.....#.......##.....#.#.... +.......#.#......#.....#..#..##. +..........#........#........... +..#.#.......................... +.#.##..#.#...#...#.........#... +.....#....#.....#..#.....#..... +...#.#.#.....#.#..#.......#..#. +.....#...###...##...#......##.. +#.###......#.#...#.#.#..###.... +#.....#..##......#..........#.# +#...............#........#.#..# +.....#..#.........#......##.#.. +.....#.##.##..#..##............ +...#......##...............#.#. +.#..#.#............##.#........ +#.....#..###.............##.#.. +...##..#.#..#...........#..#... +#....#.........#.#............. +##.#.........#..###......#.#..# +...#...#......#.#.#.##..#.##... +.....##............#.##.##..#.. +....#................#.##..#..# +...#..#.......#...#..#........# +....#...#...#................#. +....##...............#.#...#... +.#.....###...#.......#.##...... +....######.#..............###.# +.#..#.........##............... +................##.#..#....###. +.......#............#.#..#..#.. +......#.#...............##.#... +...#..####.#...#..#..#......#.. +....#.#...#.....#.........#..## +.##..#...#......##....##.#.#... +.##.#.........##...#....#...... +..#.#..#...#.#..#.......#...#.# +.........#..#.....##..#........ +..#......#..##.....#..#...###.. +..#...#....#.#..#..#.#.#..#.#.. +...#..#####.....#......#....... +#.#............#......#..#...#. +.........#..........###.......# +......#....#..#.##.#......#..#. +...........##.#....#.#..#...... +..#...................#..#.#... +#....##.............##....#...# +##..#....#.........#..........# +....#.#.#...#..#........#.##..# +...............#...#..##..#.... +.##.......#.......#...........# +#.........................##... +#........#.#..#..##..####.#.... +...................##.....###.. +.#.......#..#......#......#...# +..#.........#...#..........#... +..........#......#....#........ +.#......#..#...#..#...##....##. +...#.#..#..#......#.....##.#### +.......#.#....#.......#........ +#...#.#...##..##.#......#...... +.#.........#................... +...#..........#.#......#....... +...#.....##....#..........#.... +.#..........##..#..#..##....#.# +.##.#..........#...#.##.......# +#...###....#..#.#...#..#....... +..................##........... +..#...##.#...........#....#.##. +..#......#..##..#....##..#...#. +..#....#.....#.##..#.......#..# +#...#....#..#.#....#......##... +.......##..#..........#........ +..#.............##.#.....#...#. +...............#....#...#...##. +##...........#.......#.##...... +#..#...........#.........#..... +....###.............###.##..##. +.........#.#.....###.......#... +..#.##....#.#..........#....#.. +#........#....##...#..#........ +......#..........###..#.#...... +.....#.#......##.....#..##...#. +.#.......#......#...#...#...#.# +.#..........##.......#.....##.# +###.#...#....#.....#...#......# +..#.#.#..#.##.#..#............. +.....#......................... +.#..###..#...#...#..#..#...#.#. +#................##...##.##.... +......#...#...#..........#...#. +..........#.....##............. +..#.#......#........#.......#.. +........##.............#....... +.......#......#.##.#..#........ +#.#.#....#........#..........#. +##..##......#..#..#.....#.#..## +##..#..........#............... +#.....##...#.#......#.......#.# +#.....#...#....#..#.....##..... +##..........#.#.....#....#...## +..##.###..#.....#.......#...#.. +.#.#.......#......###........#. +.#..............#.#..###....... +.#....#..##.........#..#.#..... +....#....#.#....#..#.......##.# +#.......#.......#.........#.... +...#....#....#.....##..#..#.#.# +........#....#...........#..... +.#......##.#.#.##.............. +#..#.#.....##........#........# +##...#.#.......##.......#...#.. +#...#.....#.##...##.#.....#.... +....#..##...#........#.#...#... +...#....#.#.#..###...##.#.....# +......#..#.....#..#........##.. +.......#.....#.#.........#.#..# +..#.......#.#.#.#.#....#.##...# +.#...#........#..##..#......#.. +.#..#............#...#..#.#.... +...##......#......#............ +..#...#.#.....#.....#..##.#.... +.#......#.#......#..#.#........ +..#..........##...#.#.....#..#. +#...#.....#..#...#............. +..##.................#....#.... +.#....#.......#..##....#......# +.#....###............##....##.# +##..#........#..#...#.......#.. +.....#.....#.#.#.##.........#.. +.......#..#....#...#...#....... +...#...#...#.#.#..#.#.....#.... +#.#........#..#.##..#..###..... +..................#..#......... +#.#.....#..##.........#.......# +###..#.......#..............#.. +......#..#.....###..........#.. +....#.#...#..#...........#.#... +...#.....#.......#.....#.#..... +#.....##..#......##...........# +#...###...........##..#...#.##. +......##.##.#...#..#....#...... +...#.#......##.#......##....#.# +..............#.#.###.......#.. +........#....#.......##..#..### +...#.....##.#....#......##..#.# +..##........#.....#.#..#...#... +.#..#.##.........#.....#...#..# +..#..#....#...........#........ +.#...#....................#.... +##.....##....#.............#.#. +....#.#..#.#..#.#.#..........## +.............##.#.....#..#..#.. +.#....#.....##...#.###......... +..#........#........#.#..###... +.##....#...#...#.......#...#.#. +..#...#...#..##........#..#.... +..##.#..#..#.....#......#.#..#. +.#........#..#....#..#......... +..#.#.....#.##..#........###.#. +.....#.##.....##.#............. +#.........#.......#...##...#... +..#.##.#..#..#............#.... +.##....#..#............#.....#. +###........##.....##.#...#..... +#......##..##.#.#.#.#.#.#..##.. +.....###.....#....#......#....# +........#.........##...#....#.# +.#.#.....#.#..#..##......#...#. +...#.##....#..#.###..#..##..... +....#..........##..#..#..#..#.. +...#..#.##..#..#....#.........# +.....#..###.#.....#.....#..#... +......#...#....#.##...#.#...... +.#.###..##.....##.##......##... +.....#.#...........#.#......... +#........#...#..#......##.#.... +..#.......##....##....#.##.#..# +...###.#.........#......#.....# +..#.##..#....#.....##...#.##... +....##.##.............#...#.... +##..#...#..#..#..#............. +.....#.....#.....#............. +...#.##.......#..#.#.....#....# +#.....##.........#......##..... +.....##..........#..#...#..#... +#...###....#.......#...##...... +.#....#..#......#.....#...#.#.. +#........#.#.#...#.....###.#.## +##...#...##..#..#....#......... +....#............#..#.....#.... +#......#.........##....#....... +.#..#..#........#.............# +.##..........#......#.......#.. +#............#..#....#......... +....#.#.....#.##...#.....#.#... +...#.#..#...##..#...#.#.#...... +#....#..#.........##..#.#.#..## +.#...#..............#.......#.. +#...#.....#.#........##......## +...#....##.####.#.........#.#.# +....###.#..#............#.#..#. +....#......#...#......##.#.#.#. +.....#..#.#.##.#...##.......... +##..#...#.#...###.............# +....#...#..#.....#.#..#..#..#.. +#..........####......#.....###. +.........#........#.##.#...#... +.........#..........#.#..###... +.....##........##.........#...# +..##....#...#.......##......... +.....#.#......##....#...#...#.. +.##..#..##..................... +.......#...#..#..#...##....#... +.#...#.......###...#..#..#..... +.......#.....##.##.#.......#..# +.##......#...#....#..#......##. +.##....#..#....#...#...#....... +.........##..#..#.#.#.....##... +...#..............#..#.....#### +.#.#.#..#.......#.......#...... +..#.#......#..........#........ +.#...#.#..#.......#..#..#..#... +.......##.#...#..#....#.....#.. +.##...##....##...#........####. +....#.#..##....#...#....#.#.... +.....#.....#..#..#.#.##..#..... +..#....#..............#....#... +..#.#.#.....##.#.....#..##..... +....#.....#....#...#...#..#.#.. +#...#...........#..#..#........ +...#.#..#.........##.#...#..##. +......#.#.........#.#...#...... +......#..##.###......##.#....#. +.....#...#..#.......#.......... +.#...#.......#.....###......#.. +...........##.....#..#..#....#. +..#....#..#...#......#.......#. +..#...#...#.#..#....#...#...... +.......#....###.####...###.#... +#.##.#.......#.......#....#.#.# +.##..........#.....#..###...... +.....#...........#.##..#....#.. +........##.....#.#........##... +#..#..#..................##.... +#...###..........#............. +.......#.#.......#.#.......##.. +.....#.#...#....#...####.....#. +..##.....##.......#....#....... +##..........#...#..##....##.... +..........#..#......#........#. +##..#....#..#....#.....##....#. +##.##.....#...##.##.......#.... +..#..#.###.#..##.#..#..#...#... +.#..#.....#........#...##.#.... +..#..#.....#.#......##.#.#..... +.#..##...#.#....#...#...#.#.##. +.........#...#....###.#.....#.. +...........###.#.#..#..#...#.#. +##...#......##...........#..#.. +.........##..#...#.......#..... +#......#.#..........#..#....... +...#.................#....#.... +#....#......................##. +##.......#..#......#.#...###.#. +..#....#..#.#......#........... +...#...........###.#.#......... +..#..##.....#.....##...##...... +..#..#.#.#.#..#..#..##....#...# +#......##.....##..##.##...#.... +#.....#.....#.#........#....... +.#.....#.................#....# +.###....#...#............#.#.#. +.#...#.#......#.#.............. +....#...#......#.....#.......#. +........#.....#..........#....# +#..#......#...#...#.........#.. +#....#......#...##.#...#...#... +#...#....#....#..#..#.....#..#. +#......##..#..#.#.#..#.#....... +..#..#...............#...##...# +............#..............#.## +.#.#.#......##.......#.......#. +....#.........##.......#...###. +.......#.#...#.#.#.......#..... +....#..#..#...#....#.##.#.##... +...##.##.#...#......#.......... +#.....#...#.#...#.##..##.#..... +.......#.....#...#.#...##.#.... +.#.............#.....#....##..# +##......#.......#...#....#..... +.###......#.................#.. +#.#......##.........##..#...... +...#....#..........#.#......... +..##..#.........#.............. +.....#...#..................#.# +.............#.........#...#..# +....#....#......#.#.......#...# +#..#............#.#.......#...# +..#.....#............#......... +.#.....................###....# +........#.####.........#.#.#... +#...........##...#.........#..# +...........#..#......#...#.#... +....##...##.....#.....#........ diff --git a/2020/samples/03.txt b/2020/samples/03.txt new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/samples/03.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/2020/src/common.rs b/2020/src/common.rs index 17eb0b8..84fca41 100644 --- a/2020/src/common.rs +++ b/2020/src/common.rs @@ -34,6 +34,30 @@ where buf.trim().parse().unwrap() } +pub fn read_char_grid(input: &mut dyn Read) -> Vec> { + let mut reader = BufReader::new(input); + let mut buffer = Vec::new(); + + let mut grid = Vec::new(); + + while let Ok(read) = reader.read_until(b'\n', &mut buffer) { + if read == 0 { + break; + } + + let line: &[u8] = if let Some(&b'\n') = buffer.last() { + &buffer[..(buffer.len() - 1)] + } else { + &buffer[..] + }; + + grid.push(line.to_owned()); + buffer.clear(); + } + + grid +} + /// An interface to count elements in particular categories. pub trait GroupingCount { /// The type of the categories under inspection diff --git a/2020/src/day03.rs b/2020/src/day03.rs new file mode 100644 index 0000000..b05e1d1 --- /dev/null +++ b/2020/src/day03.rs @@ -0,0 +1,60 @@ +use std::io::Read; + +use crate::common::read_char_grid; +use crate::Solution; + +#[derive(Default)] +pub struct Day03; + +fn walk(right: usize, down: usize, forrest: &[Vec]) -> usize { + let mut trees = 0; + let mut x = 0; + + for line in forrest.iter().step_by(down) { + if line[x] == b'#' { + trees += 1; + } + + x = (x + right) % line.len(); + } + + trees +} + +impl Solution for Day03 { + fn part1(&mut self, input: &mut dyn Read) -> String { + let forrest = read_char_grid(input); + + walk(3, 1, &forrest).to_string() + } + + fn part2(&mut self, input: &mut dyn Read) -> String { + let forrest = read_char_grid(input); + let mut product = 1; + + for &(right, down) in &[(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] { + product *= walk(right, down, &forrest); + } + + product.to_string() + } +} + +#[cfg(test)] +mod tests { + use crate::test_implementation; + + use super::*; + + const SAMPLE: &[u8] = include_bytes!("../samples/03.txt"); + + #[test] + fn sample_part1() { + test_implementation!(Day03, 1, SAMPLE, 7); + } + + #[test] + fn sample_part2() { + test_implementation!(Day03, 2, SAMPLE, 336); + } +} diff --git a/2020/src/lib.rs b/2020/src/lib.rs index f3b6e48..a000fab 100644 --- a/2020/src/lib.rs +++ b/2020/src/lib.rs @@ -17,6 +17,7 @@ pub fn get_implementation(day: usize) -> Box { match day { 1 => Box::new(day01::Day01::default()), 2 => Box::new(day02::Day02::default()), + 3 => Box::new(day03::Day03::default()), _ => panic!("Unsupported day {}", day), } }