diff --git a/2020/03/Cargo.toml b/2020/03/Cargo.toml new file mode 100644 index 0000000..1b3450a --- /dev/null +++ b/2020/03/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day03" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.9.0" diff --git a/2020/03/input b/2020/03/input new file mode 100644 index 0000000..b4223c3 --- /dev/null +++ b/2020/03/input @@ -0,0 +1,323 @@ +...#...#..#....#..#...#..##..#. +.#..#.....#.#............###... +.#...###....#.............##..# +...##...##....#.....##..#.##... +.....###.#.###..##.#.##.......# +#...##.....#..........#..#.#.#. +......##.......##..#....#.#.... +....#.###.##..#.#..##.##....#.# +.......#.......###.#.#.##.....# +.........#.#....#..........#.#. +.#...##.....##.........#..#.... +.##....#.#.#...##......#....... +##.#.#..#....#....#....#...#.#. +##....#.#..##......#....##...#. +....#..#..##..#.###.......#.#.. +.....##....###...........#.#.## +#.....##.........#....##......# +........###.#..#....#....#..... +...#.......#.##..#.###......#.. +...............#..#....#.##.... +..#..###..#.#..#.........##..#. +####..#..####.................. +#...####...#.......#.#.#...#... +......###.....#......#..#..#... +#...#.....##.....#.#..##...#.#. +#...........##.......#......... +.#..#.........#.#..##....#..... +........##...#................# +........#.###.#.###.#.#.##..##. +.#....##.....#...##.#..#.#..... +..#..#.....###....##.#....#.#.# +#......##.##...##..#.........#. +#..#..#.....#.....#.........#.. +#....#.#...###.........#...#... +.#.#.....##......#.#......#.... +..##......##...#.#.#.#......... +..#......#.....##.###.#.#..#... +....#..#.......#..#..#.....#... +.#.#.....#...#..........#...... +#.#..#...........#.#.##.#...#.# +..#.#....###...#...#.....#.#... +....##.#.###....####.......#... +.....##....#.......#..#..#....# +...##..#.#.#.#......#......#... +...##...#....#...#......###...# +........#..#.#.....#.###....... +..#..##.#....#.#.........#...#. +.....#.####....#.##.........#.. +......#...#...#.....#......###. +.##.....#....#..#.#....#.....#. +...........#...#....##..#...#.. +.....#....#.....#...##..#...#.# +.#...#.........#.......#...#..# +...#..#...#........#......#.... +..#..#####.#.....#.#....#...#.# +...#.......#.#....#...##..#..#. +####..#.#.###.#.#.............. +.##........#...#.#....#..#..... +..#..............#.#..##...#.## +.###.#.....#.#.....##.#......## +....###.....#...#...#.#..#..... +....###.#.##.......#....#...#.. +#..#...#......##..#.....#.#...# +....#.#.........#.............. +#.##.##...#..#.#.#.....#...#.## +#...#...#......#...........##.. +#.#.#......#............#.#.... +.#.#..######...#.#.........#.## +..#.#..#...#......#............ +....#.....#......##..#.....#... +.##............#....##..#...... +.#.#.#...#.##.............###.# +#.#...#...#.....#....#.#.#..... +........#..#......##.##.#.....# +.....#.....#.#####...#....#.... +.#...#......#.........#.#...... +...#...#..##.....##....#..#.... +....#....##..#.........#....... +..#........##..#.#........#.... +...#...##...........#...#....#. +.....##.........#..#....#..#.#. +#..#....##..#...##.....#..##.#. +..#.#.#.#...#...#.....#.#....#. +.......#.###...#.#.......#.#... +....#..#..#.###.#.....###..#.#. +.#..##......#..#..#....#.####.. +..##...........#...#.........#. +......#..#...#..........#...... +....#..........#......##...#... +....#..#.##........#.#...##.#.. +#.##......#........##.#...#...# +#..#....#.....###........##.... +...........##.....##..#....#.## +..#....#..#..#......#.#.....#.. +#....#.##....#.....##.......#.. +.#.....#.#..............#.##..# +.#..#..#...#...#....#.#.....#.. +...###...##.#...#..#........#.. +#...#.##.#.....#.#....#..#..... +#.....###.#.......#.#..#.#..##. +....#..#..##.......###.#...#... +.#...####...............#.....# +.#.##.#.....#.....#.#......##.# +#...........#.##....###.##....# +...............#..........#.... +.....#..#.##.###.#............. +...##.............#.....#.#..#. +....#.#...#.#..#..#..#....#.... +..#.......#..........#...#...#. +...............#.#.#...###....# +....#...#.##....#..##....#..... +........#.#.##.........##.##.## +#.....###.......#.#....#..#..## +.#..#...#......#.#..##.......#. +#.....#.#........#.##..#..#.... +.###..##.#.......#......###.... +.#...###.....#.....#....###...# +........##.##......#.#....#...# +.#....#..#.........#..##...##.. +.......#.......##.#..#..##..... +#..##..##......#.#......#.##... +..#..###..#...#....#..#...#.... +#.............#.####.........## +..#..................#...#..#.. +..#......#........##.......#.#. +.#.#.#.#..###.....#....#.#..... +...#.##.###.......#....#....... +................##...#.....#... +..#.###.#...#.####....#..#..#.. +..#....###....##..#.#.........# +.#..#.#.....#........#....##... +.....#..#......#..#..##.#.#.... +.#..#.........##....##......#.. +.....#.#...#...#.#...#.#...#.#. +..#..#...#...#...##.#..###..... +..#..##......#..##.#...##...... +.......#..##....##.#......#..#. +..#......#.#.....#.##....##.... +..#....#......#......##........ +....##.#.#....#.......#.##..... +#.....#...###....#....#...#.... +............#.#..#...#...#..#.. +..##.............##....#....... +.#.......#.##.#......#....##... +...##............#....#..#...#. +.##.####.....#.#..###.#....#.## +....##.#........#..#...#....... +...#...###.##...........##..#.. +..##..##....#...#..#..........# +..#.........#.#...##..........# +.......##....#.#...##.....#..#. +.............#.....#.#.......#. +#.......#..##..##...##.#....... +..............#.....#.#..#...## +........##..#.....#...#...#.#.. +###.#.................#........ +...#........#...#.#######..#..# +...#.##...##.#.#..######...#... +#.......#..#....#..#.##.....#.. +#..#....##....#.##.......#....# +#...#..#.#.#...#..#.##..#...... +....#..##....#..#.#...........# +.##..#.#.............###....... +#....##......#..#..#.....###... +..#..........#...###.#......... +.####......#....#......#.#....# +..#....#.#.#......#....#....... +.....#.....#....#....#####....# +.##..........#...#.###....#.... +....##.....##......#...#.#..... +.#...#...#..#.#.#...#####...... +...#.##..####.##.##.......##... +............#.......#.......... +.#..##.#..#####........#..#...# +#......##..##..##.........##... +....#....#.............#.#....# +###..#.....#.....#.#...#..#.### +#...#.......##......#....#.#.#. +...#......#..#...#....#...###.# +....#....##.......#....#......# +............#......##.##.....#. +...#.........#......#....##..## +.....##....##...#..###...#..#.. +.......##.#..........#.##.##... +....##...........#.#..#..#.##.# +#...#..##.##.#....#....#.#..... +...##.#.....#..#..#..###....##. +#.##.#..#..#.#.............#... +..#.#.............###.....#.... +...#..#....#..#.....#.#..#..#.. +...#.....##.#...........#..##.# +.........#.#.##..#..#.#...#.... +...#..##..#...#...###.##.#..#.. +.#..##...##......##..##........ +......##....##.#.##.#.#........ +...#..................#.....#.. +.##................#.#..#..###. +.##.##.....#................#.. +.....#.#..........#...#..#.#..# +.............#......#..#.#..#.. +...#...##..#........#....#..... +#......#........##.##...##..... +##..#..##....#...#............# +..##..##.##....##..##........#. +...#....#.#.#.#....#.#...##.... +....#...##..##.#.##...#..#...#. +#..#....##.#.....#.......#...## +##.#....#.............#..#..... +.##..#..#.#.....#.......#.#..#. +.......#..#...##...#...###..#.. +..........#...#.#..##.....#...# +..#....#...........#####....#.. +#....#..#.......##............. +.........##..#####.......##.... +#..#..........#.....###...#..#. +.#.#.#..#...#.......##...#####. +.....#....#.###...#.......#.... +#.#.....##...###....###....#... +.#.....#..#.#.#........#...#... +.##.#.#.#......#....###....#... +.#..##..####......###......#... +......#.#.#.#.#...#...####.##.. +.#........##..#.....#....#....# +.....###......##..#....#....... +#.#.##...#.#......###.......... +........#.#...#..#......#....#. +..##...##.........#.......#.#.. +..#.##....#...##.....#.###..... +.........#..#.#....#....#.#.##. +#.........#......#..#.......#.. +...#...##.......#.........#.... +............#......#........... +##.....#.....#.#...#.....#..... +..#.#...#..#...#.#...........#. +#.#.#..#..#...##.#...#.#.....#. +.#..###.#..##.#.....#.....#.... +##....##....#.......##..##..... +.#..#...........###..........#. +.#..#..#..........###..#....... +#..###......#............##...# +#......#........#..#..#..#.#... +.......#.###...#.##............ +.##....#.......#.#...##.....#.# +....#..#.#.......#.#........... +##....#.###.#....#.#..##.#....# +..#..#..#....#...#........##... +...#...##....#..#.#...#..#..... +......#..#......#....#.......#. +#.#..............#...###...#..# +...#....#..#..........#.#...#.. +#.....##..##.....#........#.... +.#...##..#.#..............#.... +##.#....#..##...#..#.####.#..#. +.....#.......#.#.#.#..#.....### +...#.##....#.#........##....... +#...#.#...#.#..###..##.##...#.# +###..............#.#.###....... +...###..#.#..#....##...###.#... +......##...........#...#..#...# +.#..#.........##.......#..#...# +.#.......###......##...#...#... +.#......##...#........#.......# +.#..#.....#.........#.#........ +#...#.#.....#...#..##.........# +......##.#......##.#..##.#..... +...............#.#..#....#....# +#....#..#..#..#.#.....##...##.. +#.#......#.###......#..#...#### +.#.#..#...#...#.#..#.##.##.#.#. +.....#.#...###...#.#.....##.... +...#..#.#..........##.#....#.#. +...#..#.#.##.....###.##.#....#. +..........#..###......#..#.#... +###.....#..###..#...#..###.#... +..#..#.....##.#.#..###.......#. +....#....##........##.......... +.......#..........#...#......#. +.#........#.#.#.#.#.......#.... +.#..#.......##..##....#.#...#.. +.#.#.#.......#..#.............. +#.#....#.#...#.#.#.....#.#...## +.....#..........##..#.......#.. +.##......#.#....#.#.......#.... +..#.##....#.##.#...#........... +...##......##..##.............# +..........##.#.#..#..........#. +.##....#..#..#.#....##.#...#.#. +...........#....#.....#.#..#... +.#.....#....##..#.........#.... +.....#.....#...#....#...#.###.# +..#....#....#.....#...#......#. +.....##..#.............#...#... +........#..#.......#.#.......#. +#...###..#.##.#...###...##..##. +....##..#.......#...#.#........ +.#...#.#.##....####........#..# +.#...#.#.####.##.#............. +#..##...#....#...#.#.#.#.##..#. +.#.......#........#.....###.... +#.#.....#....#..#....#..#....#. +...#..#...#.....#.........##... +.#....#......###...#....#.#.#.. +#.#........#......#...#....##.. +.....#..#......#..#..#......#.. +.#.....#..#.##.#.#.#...#......# +##........#..#.#..#...#.####... +..........##....#.#..#.#....#.. +#.##..#..#....#..#....##..#.#.# +..#......#.......#...##..#..... +##...#.........#......#......#. +.#.....................#..#.##. +.#.......#........#.#.#..##.#.. +..#..........#........#..##.#.. +.#...#...#.........##.#.#.#.... +....#....#.###.#....###....#.## +....##......##........##.#.##.. +....#.#......#.##.#...#.##..... +....#....#..#.#..###.#.#....... +....#......#..#.#.......#..##.. +.....#..#.#.##.##..##.....#.#.. +...#....................##..... +#.....#...##...#.#............. +..#.#...#.#.#.....##..#....#... diff --git a/2020/03/src/main.rs b/2020/03/src/main.rs new file mode 100644 index 0000000..2d0c20b --- /dev/null +++ b/2020/03/src/main.rs @@ -0,0 +1,94 @@ +#![feature(test)] +extern crate test; +use itertools::Itertools; + +#[derive(Debug, PartialEq)] +enum Tile { + Free, + Tree, +} + +type Forest = Vec>; + +const STEP_RIGHT: [usize; 5] = [1, 3, 5, 7, 1]; +const STEP_DOWN: [usize; 5] = [1, 1, 1, 1, 2]; + +impl From for Tile { + fn from(c: char) -> Self { + match c { + '.' => Tile::Free, + '#' => Tile::Tree, + _ => unreachable!(), + } + } +} + +fn read_input() -> String { + std::fs::read_to_string("input").unwrap() +} + +fn parse_input(raw: &str) -> Forest { + raw.lines().map(|l| l.chars().map_into().collect()).collect() +} + +fn count_all_paths(forest: &Forest) -> usize { + STEP_RIGHT.iter().zip(STEP_DOWN.iter()) + .map(|(&r, &d)| count_trees(forest, r, d)) + .product() +} + +fn count_trees(forest: &Forest, step_right: usize, step_down: usize) -> usize { + let mut x = 0; + let mut y = 0; + let mut trees = 0; + let width = forest[0].len(); + while y < forest.len() { + trees += (forest[y][x] == Tile::Tree) as usize; + y += step_down; + x = (x + step_right) % width; + } + return trees; +} + +fn main() { + let forest = parse_input(&read_input()); + let p1 = count_trees(&forest, STEP_RIGHT[1], STEP_DOWN[1]); + println!("Part 1: {}", p1); + let p2 = count_all_paths(&forest); + println!("Part 2: {}", p2); +} + +mod tests { + use super::*; + use test; + + const INPUT: &str = "..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.#"; + + #[test] + fn part1_test() { + let forest = parse_input(INPUT); + assert_eq!( + count_trees(&forest, STEP_RIGHT[1], STEP_DOWN[1]), + 7 + ); + } + + #[test] + fn part2_test() { + let forest = parse_input(INPUT); + assert_eq!( + count_all_paths(&forest), + 336 + ); + } +} diff --git a/2020/setup_day.sh b/2020/setup_day.sh index 05aa3fa..ed60f35 100755 --- a/2020/setup_day.sh +++ b/2020/setup_day.sh @@ -5,6 +5,7 @@ mkdir "$today" cd "$today" cargo init --name "day$today" echo 'Initialized cargo project' +cargo add itertools # this assumes that your puzzle input is already in your clipboard xsel -b > input