diff options
| author | 2020-12-12 03:07:25 +0100 | |
|---|---|---|
| committer | 2020-12-12 03:07:25 +0100 | |
| commit | 35536a73389e9d555e42f9388552b7c43f5c3a8b (patch) | |
| tree | df98e8043543fdffdb767128b7a5d97b998a1b27 /day3/__init__.py | |
| parent | 4985ee94450df0bbcf982b7652c946a47707e60c (diff) | |
| download | 2021-35536a73389e9d555e42f9388552b7c43f5c3a8b.tar.gz 2021-35536a73389e9d555e42f9388552b7c43f5c3a8b.tar.bz2 2021-35536a73389e9d555e42f9388552b7c43f5c3a8b.zip | |
Added day 3
Diffstat (limited to 'day3/__init__.py')
| -rw-r--r-- | day3/__init__.py | 43 |
1 files changed, 43 insertions, 0 deletions
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 | |||
