diff options
Diffstat (limited to 'day1/__init__.py')
| -rw-r--r-- | day1/__init__.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/day1/__init__.py b/day1/__init__.py new file mode 100644 index 0000000..d7c995c --- /dev/null +++ b/day1/__init__.py | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | from abc import ABC | ||
| 2 | from itertools import groupby | ||
| 3 | from typing import Iterator, Any, List | ||
| 4 | |||
| 5 | from aoc import BaseAssignment | ||
| 6 | |||
| 7 | class Assignment(BaseAssignment, ABC): | ||
| 8 | def calculate_elf_calories(self, input: Iterator) -> List[int]: | ||
| 9 | return [ | ||
| 10 | sum([ | ||
| 11 | int(i) | ||
| 12 | for i | ||
| 13 | in group | ||
| 14 | ]) | ||
| 15 | for in_group, group | ||
| 16 | in groupby(input, key=bool) | ||
| 17 | if in_group | ||
| 18 | ] | ||
| 19 | |||
| 20 | |||
| 21 | class AssignmentOne(Assignment): | ||
| 22 | example_result = 24000 | ||
| 23 | |||
| 24 | def run(self, input: Iterator) -> int: | ||
| 25 | return max(self.calculate_elf_calories(input)) | ||
| 26 | |||
| 27 | |||
| 28 | class AssignmentTwo(Assignment): | ||
| 29 | example_result = 45000 | ||
| 30 | |||
| 31 | def run(self, input: Iterator) -> int: | ||
| 32 | return sum( | ||
| 33 | sorted( | ||
| 34 | self.calculate_elf_calories(input), | ||
| 35 | reverse=True | ||
| 36 | )[:3] | ||
| 37 | ) | ||
| 38 | |||
| 39 | |||
