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