diff options
Diffstat (limited to 'day3')
| -rw-r--r-- | day3/__init__.py | 39 | ||||
| -rw-r--r-- | day3/example.txt | 4 | ||||
| -rw-r--r-- | day3/test_init.py | 22 |
3 files changed, 65 insertions, 0 deletions
diff --git a/day3/__init__.py b/day3/__init__.py new file mode 100644 index 0000000..9025f4a --- /dev/null +++ b/day3/__init__.py | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from typing import Iterator | ||
| 4 | |||
| 5 | from aoc import BaseAssignment | ||
| 6 | |||
| 7 | |||
| 8 | class Assignment(BaseAssignment, ABC): | ||
| 9 | @classmethod | ||
| 10 | def parse_item(cls, item: str) -> Iterator[list[int]]: | ||
| 11 | yield [int(i) for i in item] | ||
| 12 | |||
| 13 | @classmethod | ||
| 14 | def find_highest_joltage(cls, item: list[int], n: int) -> int: | ||
| 15 | values = list() | ||
| 16 | item_length = len(item) | ||
| 17 | |||
| 18 | last_index = -1 | ||
| 19 | for end_index in range(item_length - n + 1, item_length + 1): | ||
| 20 | start_index = last_index + 1 | ||
| 21 | value = max(item[start_index:end_index]) | ||
| 22 | last_index = item.index(value, start_index, end_index) | ||
| 23 | values.append(str(value)) | ||
| 24 | |||
| 25 | return int("".join(values)) | ||
| 26 | |||
| 27 | |||
| 28 | class AssignmentOne(Assignment): | ||
| 29 | example_result = 357 | ||
| 30 | |||
| 31 | def run(self, input: Iterator[list[int]]) -> int: | ||
| 32 | return sum([self.find_highest_joltage(item, 2) for item in input]) | ||
| 33 | |||
| 34 | |||
| 35 | class AssignmentTwo(Assignment): | ||
| 36 | example_result = 3121910778619 | ||
| 37 | |||
| 38 | def run(self, input: Iterator[list[int]]) -> int: | ||
| 39 | return sum([self.find_highest_joltage(item, 12) for item in input]) | ||
diff --git a/day3/example.txt b/day3/example.txt new file mode 100644 index 0000000..7255fca --- /dev/null +++ b/day3/example.txt | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | 987654321111111 | ||
| 2 | 811111111111119 | ||
| 3 | 234234234234278 | ||
| 4 | 818181911112111 | ||
diff --git a/day3/test_init.py b/day3/test_init.py new file mode 100644 index 0000000..ae4033e --- /dev/null +++ b/day3/test_init.py | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import pytest | ||
| 3 | |||
| 4 | from day3 import AssignmentOne, AssignmentTwo, Assignment | ||
| 5 | |||
| 6 | |||
| 7 | class TestAssignment: | ||
| 8 | data = [ | ||
| 9 | ["987654321111111", 2, 98], | ||
| 10 | ["811111111111119", 2, 89], | ||
| 11 | ["234234234234278", 2, 78], | ||
| 12 | ["818181911112111", 2, 92], | ||
| 13 | ["987654321111111", 12, 987654321111], | ||
| 14 | ["811111111111119", 12, 811111111119], | ||
| 15 | ["234234234234278", 12, 434234234278], | ||
| 16 | ["818181911112111", 12, 888911112111], | ||
| 17 | ] | ||
| 18 | |||
| 19 | @pytest.mark.parametrize("battery,n,joltage", data) | ||
| 20 | def test_find_highest_joltage(self, battery: str, n: int, joltage: int): | ||
| 21 | banks = next(Assignment.parse_item(battery)) | ||
| 22 | assert Assignment.find_highest_joltage(banks, n) == joltage | ||
