From 5149637c976263eee9a332175873c61102d7c8ac Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Dec 2020 08:48:43 +0100 Subject: [PATCH] Solve day 3 --- 2020/day03-toboggan.py | 47 ++++++ 2020/inputs/day03 | 323 ++++++++++++++++++++++++++++++++++++++++ 2020/inputs/day03-tests | 11 ++ 3 files changed, 381 insertions(+) create mode 100644 2020/day03-toboggan.py create mode 100644 2020/inputs/day03 create mode 100644 2020/inputs/day03-tests diff --git a/2020/day03-toboggan.py b/2020/day03-toboggan.py new file mode 100644 index 0000000..2a6b204 --- /dev/null +++ b/2020/day03-toboggan.py @@ -0,0 +1,47 @@ +from collections import Counter +from math import prod +from typing import List, Tuple + +TREE = "#" + + +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + forest = f.read().strip().split("\n") # type: List[str] + + trees_31 = trees_for_slope(forest, right=3, down=1) + print(f"Found {trees_31} trees for part 1") + if expected_part_1: + assert trees_31 == expected_part_1 + + trees = [ + trees_31, + trees_for_slope(forest, right=1, down=1), + trees_for_slope(forest, right=5, down=1), + trees_for_slope(forest, right=7, down=1), + trees_for_slope(forest, right=1, down=2), + ] + print(f"Found these trees for part 2: {trees}") + prod_trees = prod(trees) + print(f"Found {prod_trees} trees for part 2") + if expected_part_2: + assert prod_trees == expected_part_2 + + +def trees_for_slope(forest: List[str], right: int, down: int) -> int: + trees = 0 + current_line = 0 + current_col = 0 + while current_line < len(forest): + item = forest[current_line][current_col] + if item == TREE: + trees += 1 + current_col = (current_col + right) % len(forest[0]) + current_line += down + return trees + + +if __name__ == "__main__": + main("inputs/day03-tests", 7, 336) + main("inputs/day03", 240, 2832009600) diff --git a/2020/inputs/day03 b/2020/inputs/day03 new file mode 100644 index 0000000..7a7786b --- /dev/null +++ b/2020/inputs/day03 @@ -0,0 +1,323 @@ +.........#....#.###.........##. +..###.#......#......#.......##. +##....#.#.......#.....#........ +#........#..........#.#...#...# +#....###...##.....#........#... +###..............##..#.....#... +.................##.#.......... +.........##......#..###.....#.# +..#..#...#.#.#...#.#.#.##...#.. +..............#.#.#..#..#..#... +.#.#.#....#.........#.........# +..#.#....##..#...#.....#..##..# +............#.....#.........##. +...#.#..#..........#.##.....#.. +#......#...##.......###........ +.....#....#.#..............#... +.....#.......#..##.###.....#.#. +.#.....#....##.#......##......# +..##...##.........#..#.#..#.... +............#.......#.....#.... +.......................#...#### +.#.#.......#..#....#....##..#.# +..#.##.#.#...............#..... +#..##..#...#.....##..#...##.#.. +##...#....#####.....##...#...## +.#..##..#..#.#..##.#.#........# +....#..#........##......#.#.... +..#......#...##.#..##.......#.. +.#.#....#.#..#.....#..#...#.... +.....#.#.................#..... +##.#........#.....#...........# +#............#.....#..#.#...##. +..#.#..#......#.......#......## +....#.#....#...#....#.......... +.........#...#.##..#...#...#... +....#...#...#.................. +..##...#.#....#...#......#..... +#....#.......##..#...#..#...... +.#....##..#.#....#...##...#.... +#.................#...#.#...#.. +.#.....#..........#.......#.... +.#..........#.##....#.##......# +#.#.....##.##..#.......#..#.... +.....#...#............#..##.... +...#.#.##.#..........#.#....#.. +.......#...#............#.....# +..........#...##..#....###....# +............#....#......###.... +...................####..#.##.. +...#.#.##.........#..#.#....... +...........#.........#..###.... +.........#.........#...#...###. +.#.#.##....#.#...........#.#... +..###.....#...#.#.......##..#.. +.....#.#........#.#....#....##. +......#...#........#.........#. +...............#.........#.#... +..#...#...#...#.###..###....#.. +#..#.......#..###.##.......#... +#.#.........##..#.....###..#..# +...#....#....#.#..#............ +..###..##....................#. +..#.......#..........#.##...... +..##........#...###..##.#...... +......#.#...#.....#..##..#..... +#........#......#.............. +........#........#.......#....# +.....#.......#......#........## +#.#......#.#...##.#.......#.... +#...................##...#..... +..#.#...#..#...#..#.....#..#... +.......##..................##.. +.............#..##.#......##..# +###...........##.#....###..#... +.#..........##...#..#......#... +..#.###.#....#........#........ +....#....###.....#.......#..... +.....##....#..##...#..#........ +.##...#..#....#..#.........#... +#.........##....#..##..##...... +.#.#.............#.....##...... +..#.#............#.......##.#.. +..#.##..#.........#......#.#.#. +.#...#...#..#....#...#....#.#.# +....#...#..#.##..##.......##... +.#.....#.....#............#.... +..........#....#..#..#......#.. +.............#....#..#......... +....#.#.#.......#....#.#....... +..##....#.#....#...#........### +#...#..........#..........###.. +...#..#...#...#..##......#...#. +.....................#...#..#.# +#..#............#.........#.##. +..#...#...........#.......#.... +.....##..........#...##.....##. +.#.....#.#........##........... +..#....#..#.#..##...#.........# +.........#.###.##....#..###.... +.........#...##...##.#.#....##. +...#..##.#...........#....#.... +..........#.#...........##..... +...........#..........#...##... +.........#..........#...#.#...# +......#..#.................#... +.....#...........#..#...#..###. +.....#..#....#.#.##...##....##. +...##.###.#.#..#.#....##.#.#... +###.....#.....#........#...#... +.#....#........#.#....#..#...## +##.....#.....#......#.#..#...## +.##....#...............#....... +#.#.....#.#....#.........#..... +..#...............#.......##... +#...#.###..#....#.#.#..#.#..... +##.###....##...#....#.....#.... +.......#................##..... +....#....##..#....#..#......#.. +...#.........#...#........##.## +.#..............#.............. +..##.......#.###..##.#......... +..#...#...#...#...#...#.....##. +.....#..##...#.....##..#.#..... +..#.............#...#.........# +#.........................#..## +.............#..###........#... +......#..#....#.##.......#..#.. +...#..#..#...#....#..#...##...# +.##............#.......###.#... +.#........#..#................. +#.#.#.....##....##...#.....#.#. +...##.......#.#......#...#.#... +#.##....#.........#.....##...#. +#...#..#....#.......#.##...##.. +.................##.#..##.#.#.# +..#.............#.......#.#.##. +#....##..................#...## +..........#.......#..##........ +......#.#..#......#.#.........# +#.#........#####......#.#.#.... +#..#........#.#..#.....#....... +...#.............#............. +.....#.......#......#..#.##..#. +..#.........#..........#.##...# +#.....#.#####......#.......###. +.......#.....#...#.....#.#..#.. +#...#.#........#.#..#..#...#..# +...#....#....##.....#.......... +.#.......##.......##........... +...#.##.#.#..#....#...##....##. +.#...#...#.........#........... +.#.#.##..#.......#.#...#..#.... +.#....##.#.#...#......#......#. +##..#..#..#..#.......#......#.. +.........#.#...........#....#.# +........#....#.#...#.#..#...... +#.......#.#.................##. +.....#..#..#....#.#........#.## +.#..###..#....#..#........#.#.. +#...........#...#........#..... +........#..#.#.#.#.......#....# +....#..#..........#.#..#.....#. +..####..........#.............. +....###.#..#........##..#...... +.#..#......##..........#...#... +.#.....#....#......##.##...#.## +..##.#.#......#.......##....... +....##.......#..............#.. +........#.....##..............# +.#...#....##.....#....#.......# +....#.......###.......#.#.#.... +##.....##........#.....##...... +..........#.....#...##.#..#.#.. +..........#.#......#..........# +..........#...#..#...#...#..... +.#.......#..##................. +.#........##..............#..#. +.##...................#...#.... +.##....#.##.##........#........ +...##.....####.....#..#.......# +...##.#...##...#.##............ +##.......#.....###..#..#...##.. +#.####...#...#...##..#..#....#. +...#........##........#........ +#....#.#....##..#..#.##...#.... +...##....##....#.......#..###.. +..........#..#..........#..#.#. +#..#....#.......#.......#....#. +......#......#.....##..##.#..#. +##.#.....#....#.......#...#...# +..##..#.#...#...#.....###..##.. +....#..#.......#............##. +#..##.#.#.....#####....#....#.. +.#........#...#.#..##.#.####.#. +#...#...#.............#.#...... +.........#.....##..........#... +.##....#....#........#......#.# +#..###...#....#..........#..... +.#...##.........#..#..#.#...#.. +#.#.#.......................#.. +#.....#..#.#............###.... +#...#.....#.....###..#..#.#.##. +............#.........#.#.##... +...#.......###......##......#.. +.#....#.#....##......##.#...#.. +.........#.......#....#...##..# +................#.#.....#....#. +.##......#....#..#..###..#...#. +....##....#..#....#.##..#...... +.......#.#.....#..#............ +..........#....#....#..#..#.... +..#....#.....#.......#......... +......#.........#.##..#....##.# +..#..#.#.......##..#...##...... +...##..#.#.#...............##.. +..#.#.#......#....#.....#.##... +..#.....#.#...........#....##.. +###.....##..................... +.......#..#.................#.. +.#.#..#..#.........#......#...# +##.......#.##.......#.......... +#..#.....#.....#.....#.......#. +#..#.....#.....#..........#.#.. +.#....##....#.....#.......#.##. +.....#.#......##..##.#......... +#....##......#..#....#..##..#.. +#.##..#..#..............#...#.. +.#......#......#..#...........# +..#.......#........#....#..#... +.....##.#.......##....#.#....#. +........#....................#. +........#..#..........#........ +......#.#.....#.#.....#......#. +#......##......###.##......#... +...........###..#...#.......#.. +..#...###...#.....#....#...#..# +.....##......#.#......##..#.#.. +#.#......##...#.....##...#...#. +.#.#........#.......#.........# +....#....#...##..........#.#... +.#..##.#...#.#.....##......##.. +.#.....#..##....#....#....#..#. +..#.......#.#.#..........#..#.. +#.#..#....#.##....#.......#.... +........##.........#..#.#...... +.......#.....#.##..##......#.#. +.........................#.#.#. +..#..............##.........#.. +.......###.#.#.......#......... +#........#.....#.......#..#...# +##....#..#....#...........#.... +..#..#.#.#.....#.#..#....#..... +.#..##....#.##..#.............. +...#....##..#..#.##....#.#..... +...##....#......##..#........#. +....#......#....#....#........# +...#..#...#.#...........#..#..# +....#.#.#.........#...#...#.... +..........#....#......##....#.# +..##..#...##.#...###.#.##...... +#........#.##......##.#........ +..#...#.##...#..........#.#...# +...........#...........#....... +......###....#..###..##........ +...#........#..#.#............. +....#.#.....#.#............#..# +##.#.....#........#....##.....# +.......#.#...#..##.......#.#.#. +#......##..#..#...#.....#..#.#. +.#......###.....#..#.....#...#. +....#.#.............#.##....... +......#....#.....#.......#..#.. +#..#.#.#..#......#...#..#.#.... +#..............#.#....#...#.... +..#......#...##.#......#..#..#. +.......##..#.##..#.#...#.....## +.....#...........#....#.......# +.#.........#..#..........##.... +#..##..#.#......##.......#..#.. +...#....#...........##.#.#.#..# +#..#..#..#...........#....#.#.# +.....##......#......#.#...#.... +.....#..##....###.....#....##.. +........#...##......#.....#..#. +..#.#..#.#....#...#.......#.... +#.....#...#.....#.#.......#.... +......#...#.......#..##....#... +#............#.....#....#...... +..##...#.....#..#......#...#... +...#..#...#..#.......#........# +...##.#.#........#........#.... +#.#..#......##.#..#..#......#.. +#.......#..#................... +#.....#....#......#...........# +.##.#...#.#...#..............## +...###........#........#..##.#. +..##....#.#.#.##..#.#......#... +..#.#........#..............#.. +.......#.................##.... +..................#............ +....#.....#.#..............#.## +......#.....#..#......#...#.... +..#....##......#...####....#.#. +..........#...##...........##.# +...#.#..#....#......#..#....#.. +#.........###...#.....#..#....# +.#.#......##.#.....#........... +...............#.#....##..#..#. +..........#.#.#.#...#....##...# +...#....##.................##.. +#..##..#...##.##.#......#.#.### +#..#...#..#.....#...#.#..##...# +..#................#...##....#. +...#.....#.##.......##....#.#.. +....#.....#..#....##........... +...............#..........#.... +....#...#........##...#........ +...#....#...#.###.............. +#.#....#.......#..#.##.##...... +.#.....#..#..#......#....#.#... +...#..........................# +............#.#..#.##......#... +.....#..........#.#........#.#. diff --git a/2020/inputs/day03-tests b/2020/inputs/day03-tests new file mode 100644 index 0000000..8f551de --- /dev/null +++ b/2020/inputs/day03-tests @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# \ No newline at end of file