From f57d4d02c5b8050694784e948086271a7f4e49e5 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Thu, 1 Dec 2022 09:32:57 +0100 Subject: Day 1 --- day1/__init__.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 day1/__init__.py (limited to 'day1/__init__.py') 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 @@ +from abc import ABC +from itertools import groupby +from typing import Iterator, Any, List + +from aoc import BaseAssignment + +class Assignment(BaseAssignment, ABC): + def calculate_elf_calories(self, input: Iterator) -> List[int]: + return [ + sum([ + int(i) + for i + in group + ]) + for in_group, group + in groupby(input, key=bool) + if in_group + ] + + +class AssignmentOne(Assignment): + example_result = 24000 + + def run(self, input: Iterator) -> int: + return max(self.calculate_elf_calories(input)) + + +class AssignmentTwo(Assignment): + example_result = 45000 + + def run(self, input: Iterator) -> int: + return sum( + sorted( + self.calculate_elf_calories(input), + reverse=True + )[:3] + ) + + -- cgit v1.2.3