diff options
| author | 2022-12-01 09:32:57 +0100 | |
|---|---|---|
| committer | 2022-12-01 09:32:57 +0100 | |
| commit | f57d4d02c5b8050694784e948086271a7f4e49e5 (patch) | |
| tree | f5ac3cb09d1667c89e36d18080ed2fb003af5ff5 /day1/__init__.py | |
| parent | a3de698aa6b7e15e9d0974d32dc566676383bd28 (diff) | |
| download | 2022-f57d4d02c5b8050694784e948086271a7f4e49e5.tar.gz 2022-f57d4d02c5b8050694784e948086271a7f4e49e5.tar.bz2 2022-f57d4d02c5b8050694784e948086271a7f4e49e5.zip | |
Day 1
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 | |||
