From 35536a73389e9d555e42f9388552b7c43f5c3a8b Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sat, 12 Dec 2020 03:07:25 +0100 Subject: Added day 3 --- day3/__init__.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 day3/__init__.py (limited to 'day3/__init__.py') 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 @@ +from functools import reduce +from typing import List, Generator + +from aoc import BaseAssignment + + +class Assignment(BaseAssignment): + def read_input(self, example = False) -> List: + return list(super().read_input(example)) + + def slope(self, input: List, dx: int, dy: int) -> Generator: + for y, row in enumerate(input[::dy]): + x = (y * dx) % len(row) + yield (x, y) + + def get_trees_for_slope(self, input: List, dx: int, dy: int): + trees = 0 + + for x, y in self.slope(input, dx, dy): + if x == 0 and y == 0: + continue + + if (input[y * dy][x]) == '#': + trees += 1 + + return trees + + + +class AssignmentOne(Assignment): + def run(self, input: List): + return self.get_trees_for_slope(input, 3, 1) + +class AssignmentTwo(Assignment): + def run(self, input: List): + return reduce(lambda accumulator, value: accumulator * value, [ + self.get_trees_for_slope(input, 1, 1), + self.get_trees_for_slope(input, 3, 1), + self.get_trees_for_slope(input, 5, 1), + self.get_trees_for_slope(input, 7, 1), + self.get_trees_for_slope(input, 1, 2), + ]) + -- cgit v1.2.3