diff options
| -rw-r--r-- | aoc/__init__.py | 2 | ||||
| -rw-r--r-- | aoc/__main__.py | 2 | ||||
| -rw-r--r-- | day1/__init__.py | 4 | ||||
| -rw-r--r-- | day2/__init__.py | 4 | ||||
| -rw-r--r-- | day3/__init__.py | 43 | ||||
| -rw-r--r-- | day3/example.txt | 11 | ||||
| -rw-r--r-- | day3/input.txt | 323 |
7 files changed, 383 insertions, 6 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py index b07ead1..70c3ac0 100644 --- a/aoc/__init__.py +++ b/aoc/__init__.py | |||
| @@ -4,7 +4,7 @@ from collections import Iterator | |||
| 4 | from typing import Generator, Any | 4 | from typing import Generator, Any |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | class AssignmentBase(ABC): | 7 | class BaseAssignment(ABC): |
| 8 | def __init__(self, path): | 8 | def __init__(self, path): |
| 9 | self.path = path | 9 | self.path = path |
| 10 | 10 | ||
diff --git a/aoc/__main__.py b/aoc/__main__.py index da0496e..8c6a21e 100644 --- a/aoc/__main__.py +++ b/aoc/__main__.py | |||
| @@ -26,4 +26,4 @@ if __name__ == '__main__': | |||
| 26 | Assignment = getattr(assignment_day, f'Assignment{args.part}') | 26 | Assignment = getattr(assignment_day, f'Assignment{args.part}') |
| 27 | assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) | 27 | assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) |
| 28 | 28 | ||
| 29 | print(assignment.run(input=assignment.read_input())) | 29 | print(assignment.run(input=assignment.read_input(example=args.example))) |
diff --git a/day1/__init__.py b/day1/__init__.py index ce728cc..cf48f78 100644 --- a/day1/__init__.py +++ b/day1/__init__.py | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | from typing import Iterator, List | 1 | from typing import Iterator, List |
| 2 | 2 | ||
| 3 | from aoc import AssignmentBase | 3 | from aoc import BaseAssignment |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | class Assignment(AssignmentBase): | 6 | class Assignment(BaseAssignment): |
| 7 | def parse_item(self, item: str) -> int: | 7 | def parse_item(self, item: str) -> int: |
| 8 | return int(item) | 8 | return int(item) |
| 9 | 9 | ||
diff --git a/day2/__init__.py b/day2/__init__.py index 988c32e..1d65d13 100644 --- a/day2/__init__.py +++ b/day2/__init__.py | |||
| @@ -2,7 +2,7 @@ import re | |||
| 2 | from dataclasses import dataclass | 2 | from dataclasses import dataclass |
| 3 | from typing import Generator | 3 | from typing import Generator |
| 4 | 4 | ||
| 5 | from aoc import AssignmentBase | 5 | from aoc import BaseAssignment |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | matcher = re.compile( | 8 | matcher = re.compile( |
| @@ -18,7 +18,7 @@ class Item: | |||
| 18 | password: str | 18 | password: str |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | class Assignment(AssignmentBase): | 21 | class Assignment(BaseAssignment): |
| 22 | def parse_item(self, item: str) -> Item: | 22 | def parse_item(self, item: str) -> Item: |
| 23 | match = matcher.match(item).groupdict() | 23 | match = matcher.match(item).groupdict() |
| 24 | return Item( | 24 | return Item( |
diff --git a/day3/__init__.py b/day3/__init__.py new file mode 100644 index 0000000..4954c15 --- /dev/null +++ b/day3/__init__.py | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | from functools import reduce | ||
| 2 | from typing import List, Generator | ||
| 3 | |||
| 4 | from aoc import BaseAssignment | ||
| 5 | |||
| 6 | |||
| 7 | class Assignment(BaseAssignment): | ||
| 8 | def read_input(self, example = False) -> List: | ||
| 9 | return list(super().read_input(example)) | ||
| 10 | |||
| 11 | def slope(self, input: List, dx: int, dy: int) -> Generator: | ||
| 12 | for y, row in enumerate(input[::dy]): | ||
| 13 | x = (y * dx) % len(row) | ||
| 14 | yield (x, y) | ||
| 15 | |||
| 16 | def get_trees_for_slope(self, input: List, dx: int, dy: int): | ||
| 17 | trees = 0 | ||
| 18 | |||
| 19 | for x, y in self.slope(input, dx, dy): | ||
| 20 | if x == 0 and y == 0: | ||
| 21 | continue | ||
| 22 | |||
| 23 | if (input[y * dy][x]) == '#': | ||
| 24 | trees += 1 | ||
| 25 | |||
| 26 | return trees | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | class AssignmentOne(Assignment): | ||
| 31 | def run(self, input: List): | ||
| 32 | return self.get_trees_for_slope(input, 3, 1) | ||
| 33 | |||
| 34 | class AssignmentTwo(Assignment): | ||
| 35 | def run(self, input: List): | ||
| 36 | return reduce(lambda accumulator, value: accumulator * value, [ | ||
| 37 | self.get_trees_for_slope(input, 1, 1), | ||
| 38 | self.get_trees_for_slope(input, 3, 1), | ||
| 39 | self.get_trees_for_slope(input, 5, 1), | ||
| 40 | self.get_trees_for_slope(input, 7, 1), | ||
| 41 | self.get_trees_for_slope(input, 1, 2), | ||
| 42 | ]) | ||
| 43 | |||
diff --git a/day3/example.txt b/day3/example.txt new file mode 100644 index 0000000..8f551de --- /dev/null +++ b/day3/example.txt | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | ..##....... | ||
| 2 | #...#...#.. | ||
| 3 | .#....#..#. | ||
| 4 | ..#.#...#.# | ||
| 5 | .#...##..#. | ||
| 6 | ..#.##..... | ||
| 7 | .#.#.#....# | ||
| 8 | .#........# | ||
| 9 | #.##...#... | ||
| 10 | #...##....# | ||
| 11 | .#..#...#.# \ No newline at end of file | ||
diff --git a/day3/input.txt b/day3/input.txt new file mode 100644 index 0000000..6cfcb73 --- /dev/null +++ b/day3/input.txt | |||
| @@ -0,0 +1,323 @@ | |||
| 1 | ...#...#....#....##...###....#. | ||
| 2 | #.#...#...#....#.........#..#.. | ||
| 3 | .#....##..#.#..##..##.......... | ||
| 4 | .....#.#.............#..#...... | ||
| 5 | .......#...#.##.#......#..#.#.. | ||
| 6 | #.#....#......##........##..... | ||
| 7 | .....##.#....#..#...#...##...#. | ||
| 8 | ...#...#..#.......#..#...##...# | ||
| 9 | ..........#...........##....... | ||
| 10 | ..#..#..#...................#.. | ||
| 11 | #..#....#.....##.#.#..........# | ||
| 12 | .#.##.......###.....#.#...#.... | ||
| 13 | .#..##....##....#.......#...### | ||
| 14 | #.#..##...#.#..#............... | ||
| 15 | .........#....#.......##.#.#... | ||
| 16 | ...###...##....##...#..##.#..#. | ||
| 17 | ....#.........#..#...#.......#. | ||
| 18 | ....................#..#.#.#... | ||
| 19 | ..#....#..........#...........# | ||
| 20 | .#.....#..#.....##........##..# | ||
| 21 | #..##..#...##............#..##. | ||
| 22 | .#..##....#..........#..#.##.#. | ||
| 23 | ..#####..#.#............##..... | ||
| 24 | ...###.#....##..#.#....#.....#. | ||
| 25 | .#.......##....#...#.#.....##.. | ||
| 26 | ...#....#...##.#...#..#........ | ||
| 27 | .####.....#....#.#.#...#....... | ||
| 28 | ...#....#.....#.......#........ | ||
| 29 | #..#.#.......#...#............# | ||
| 30 | ...#.....###.##....#.#.###.#... | ||
| 31 | .#.........#.......#.#....##... | ||
| 32 | #.#..#...#.#...##......#..#.... | ||
| 33 | .....#...#..#.#...#..###..#.... | ||
| 34 | ......#.........#...###........ | ||
| 35 | .....#..##...#..........#.....# | ||
| 36 | ..#..#.#.##.#...#....#....##..# | ||
| 37 | ##....#.##...#.##.#..##....#... | ||
| 38 | .....#.#.#.#..#....##.#...#.#.. | ||
| 39 | .....##.......#........#....... | ||
| 40 | ...#.#.....#...#...##.#......## | ||
| 41 | ........#..#.#...#.#.....#.#..# | ||
| 42 | #..##...#.#...##..##...#.#...## | ||
| 43 | .##.#.#..#...#.....#.#.##.#...# | ||
| 44 | .#.####.........##.........#..# | ||
| 45 | .##..............#....#...#...# | ||
| 46 | ......#...#..#...#..#..###.#... | ||
| 47 | .......##...#.#.#..##..#......# | ||
| 48 | .....#....#..##..#.........#... | ||
| 49 | .....#..#.#.#........#.#.####.. | ||
| 50 | #..#.......###....##........... | ||
| 51 | #..##..........#.#......#.#.... | ||
| 52 | .....##........#...#..##....... | ||
| 53 | ###...#.##.#.#.#.#.##...##..... | ||
| 54 | ....#...#........##.#.##..##... | ||
| 55 | .#..#.#.#......#.......##..#..# | ||
| 56 | .#...#.................#....#.. | ||
| 57 | .##..#..........#..##.......#.. | ||
| 58 | .#.#.#.....#..#.#.........##..# | ||
| 59 | ...#......##...#.......#...##.. | ||
| 60 | ##...###....#.###.............# | ||
| 61 | #.....#.#..#.#..#........#.#.#. | ||
| 62 | .....#.#......##..#.#.....#.##. | ||
| 63 | .......#...........#..#.......# | ||
| 64 | ..#....#.#.#......#.....#...#.. | ||
| 65 | .....##........#..##..#..##.... | ||
| 66 | #.#........#...##....#.#..##... | ||
| 67 | #......#......#....#..#...#.##. | ||
| 68 | ....#.#.......#.#.#............ | ||
| 69 | ......####.#.##...#.#.##.....## | ||
| 70 | ..###.#.#..#.........#.####.... | ||
| 71 | .#.......#..#.#....#.#..#.#.##. | ||
| 72 | #....#....#............##...##. | ||
| 73 | ....#....#............#....#..# | ||
| 74 | ..#........#..#....#..#..#...#. | ||
| 75 | .#......##....#..........#....# | ||
| 76 | #.##.....#..........#.###.#.... | ||
| 77 | ....##...#.....#.#......#.##... | ||
| 78 | #.#.....#.......###.###..#..#.# | ||
| 79 | ..###..##.............#.####.## | ||
| 80 | #....#.....#....#..##.......#.. | ||
| 81 | .....#....#...#.#.#.#..#...#.## | ||
| 82 | ...#.....#..#....###......#.#.# | ||
| 83 | ##.........#.#..#..#.#..#.....# | ||
| 84 | .#.....#.#....#.........##..#.# | ||
| 85 | .#.#..#.###..#..#..........#... | ||
| 86 | .##....#.#.#...#......##.....#. | ||
| 87 | #.#....#....#...#...##...#..#.. | ||
| 88 | #...#........#....#....#......# | ||
| 89 | #......#...#..#.#.##.....##..#. | ||
| 90 | ....#...#......##...#..#....#.. | ||
| 91 | .#......##.##.......#.......#.. | ||
| 92 | .#...#..####...........#.#.#... | ||
| 93 | .........#...#.#.........#..... | ||
| 94 | #.##.....#.#..#.#.###...###..#. | ||
| 95 | #...##.###......#.###..##.#.##. | ||
| 96 | ...##.#.....#....#..#......#... | ||
| 97 | #....###.#..#...##.....#......# | ||
| 98 | ........###...#...#............ | ||
| 99 | ........#....#...#...#....#...# | ||
| 100 | #....#..#..#....#.#........#.#. | ||
| 101 | ##...#.....#.#..........#..#..# | ||
| 102 | #.#...##.....#........#...#...# | ||
| 103 | ##.#.#.......#...#..#.###....#. | ||
| 104 | .#.......#....##..##...#.....#. | ||
| 105 | #....#....#.....#.......#...... | ||
| 106 | .##.##.##...##...#.#.#..#..#... | ||
| 107 | #..#..#.##....#......##....###. | ||
| 108 | .......#.#.........#..##.#...## | ||
| 109 | .#..##...#....#.....#.......... | ||
| 110 | ..#.#...#......#.#..#.......... | ||
| 111 | .##....#.#.#.##.......###...#.. | ||
| 112 | ..##.#...#.#.#.#.......#..#.... | ||
| 113 | #..#.......#...#........#.....# | ||
| 114 | .....#.......#......###..#..... | ||
| 115 | ...##.#.......#.....##.....##.. | ||
| 116 | ##..#.......#.#.....#....#..... | ||
| 117 | ..#....#.##.##...#...#......#.. | ||
| 118 | .#..#.###.#....###........#...# | ||
| 119 | ....##.##...##..#..#.#....#.... | ||
| 120 | ..###...##.....##.............. | ||
| 121 | #....#...##...#....#..........# | ||
| 122 | .##........#......##...##...#.# | ||
