From 89929901e88749c8428afd0cf5684caa49a246b7 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Wed, 3 Dec 2025 17:45:22 +0100 Subject: Day 3 --- aoc/__init__.py | 3 ++- aoc/__main__.py | 2 +- day1/__init__.py | 3 ++- day2/__init__.py | 3 ++- day3/__init__.py | 39 +++++++++++++++++++++++++++++++++++++++ day3/example.txt | 4 ++++ day3/test_init.py | 22 ++++++++++++++++++++++ 7 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 day3/__init__.py create mode 100644 day3/example.txt create mode 100644 day3/test_init.py diff --git a/aoc/__init__.py b/aoc/__init__.py index 0257b69..f490bf8 100644 --- a/aoc/__init__.py +++ b/aoc/__init__.py @@ -17,7 +17,8 @@ class BaseAssignment(Generic[T, I], ABC): def __str__(self): return f"{self.__module__}.{self.__class__.__name__}" - def parse_item(self, item: str) -> Iterator[I]: + @classmethod + def parse_item(cls, item: str) -> Iterator[I]: yield item @property diff --git a/aoc/__main__.py b/aoc/__main__.py index 596abee..96a9b7f 100644 --- a/aoc/__main__.py +++ b/aoc/__main__.py @@ -38,7 +38,7 @@ def run( Assignment = getattr(assignment_day, f"Assignment{AssignmentPart(part).name}") assignment = Assignment( - path=os.path.dirname(assignment_day.__file__), **dict(kwargs) + path=os.path.dirname(assignment_day.__file__), **dict(kwargs or {}) ) start = perf_counter() diff --git a/day1/__init__.py b/day1/__init__.py index ec1c2f2..03d00ff 100644 --- a/day1/__init__.py +++ b/day1/__init__.py @@ -6,7 +6,8 @@ from aoc import BaseAssignment, I class Assignment(BaseAssignment, ABC): - def parse_item(self, item: str) -> Iterator[I]: + @classmethod + def parse_item(cls, item: str) -> Iterator[I]: [rotation, *places] = item places = int("".join(places)) diff --git a/day2/__init__.py b/day2/__init__.py index 882bcad..20e573e 100644 --- a/day2/__init__.py +++ b/day2/__init__.py @@ -7,7 +7,8 @@ from aoc import BaseAssignment, I, T class Assignment(BaseAssignment, ABC): - def parse_item(self, item: str) -> Iterator[range]: + @classmethod + def parse_item(cls, item: str) -> Iterator[range]: for r in item.split(","): [start, end] = r.split("-") yield range(int(start), int(end) + 1) 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 @@ +# -*- coding: utf-8 -*- +from abc import ABC +from typing import Iterator + +from aoc import BaseAssignment + + +class Assignment(BaseAssignment, ABC): + @classmethod + def parse_item(cls, item: str) -> Iterator[list[int]]: + yield [int(i) for i in item] + + @classmethod + def find_highest_joltage(cls, item: list[int], n: int) -> int: + values = list() + item_length = len(item) + + last_index = -1 + for end_index in range(item_length - n + 1, item_length + 1): + start_index = last_index + 1 + value = max(item[start_index:end_index]) + last_index = item.index(value, start_index, end_index) + values.append(str(value)) + + return int("".join(values)) + + +class AssignmentOne(Assignment): + example_result = 357 + + def run(self, input: Iterator[list[int]]) -> int: + return sum([self.find_highest_joltage(item, 2) for item in input]) + + +class AssignmentTwo(Assignment): + example_result = 3121910778619 + + def run(self, input: Iterator[list[int]]) -> int: + 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 @@ +987654321111111 +811111111111119 +234234234234278 +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 @@ +# -*- coding: utf-8 -*- +import pytest + +from day3 import AssignmentOne, AssignmentTwo, Assignment + + +class TestAssignment: + data = [ + ["987654321111111", 2, 98], + ["811111111111119", 2, 89], + ["234234234234278", 2, 78], + ["818181911112111", 2, 92], + ["987654321111111", 12, 987654321111], + ["811111111111119", 12, 811111111119], + ["234234234234278", 12, 434234234278], + ["818181911112111", 12, 888911112111], + ] + + @pytest.mark.parametrize("battery,n,joltage", data) + def test_find_highest_joltage(self, battery: str, n: int, joltage: int): + banks = next(Assignment.parse_item(battery)) + assert Assignment.find_highest_joltage(banks, n) == joltage -- cgit v1.2.3