diff options
| author | 2025-12-02 07:57:38 +0100 | |
|---|---|---|
| committer | 2025-12-02 09:40:04 +0100 | |
| commit | f315ef05c548da80410cb4f6665a9bba7a953f94 (patch) | |
| tree | a727c6ad862a4a9c237c320705483b3def7acc11 /day2 | |
| parent | d7e30321ae6ae4c82a8ab7455f6ce33afd719c67 (diff) | |
| download | 2025-f315ef05c548da80410cb4f6665a9bba7a953f94.tar.gz 2025-f315ef05c548da80410cb4f6665a9bba7a953f94.tar.bz2 2025-f315ef05c548da80410cb4f6665a9bba7a953f94.zip | |
Day 1 and 2
Diffstat (limited to 'day2')
| -rw-r--r-- | day2/__init__.py | 57 | ||||
| -rw-r--r-- | day2/example.txt | 1 | ||||
| -rw-r--r-- | day2/test_init.py | 41 |
3 files changed, 99 insertions, 0 deletions
diff --git a/day2/__init__.py b/day2/__init__.py new file mode 100644 index 0000000..882bcad --- /dev/null +++ b/day2/__init__.py | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import re | ||
| 3 | from abc import ABC | ||
| 4 | from typing import Iterator | ||
| 5 | |||
| 6 | from aoc import BaseAssignment, I, T | ||
| 7 | |||
| 8 | |||
| 9 | class Assignment(BaseAssignment, ABC): | ||
| 10 | def parse_item(self, item: str) -> Iterator[range]: | ||
| 11 | for r in item.split(","): | ||
| 12 | [start, end] = r.split("-") | ||
| 13 | yield range(int(start), int(end) + 1) | ||
| 14 | |||
| 15 | def find_invalid_ids(self, item: range) -> Iterator[int]: | ||
| 16 | raise NotImplementedError() | ||
| 17 | |||
| 18 | def run(self, input: Iterator[range]) -> int: | ||
| 19 | totals = 0 | ||
| 20 | for item in input: | ||
| 21 | totals += sum(self.find_invalid_ids(item)) | ||
| 22 | |||
| 23 | return totals | ||
| 24 | |||
| 25 | |||
| 26 | class AssignmentOne(Assignment): | ||
| 27 | example_result = 1227775554 | ||
| 28 | |||
| 29 | @classmethod | ||
| 30 | def find_invalid_ids(cls, item: range): | ||
| 31 | for i in item: | ||
| 32 | i_str = str(i) | ||
| 33 | if i_str[: len(i_str) // 2] == i_str[len(i_str) // 2 :]: | ||
| 34 | yield i | ||
| 35 | |||
| 36 | |||
| 37 | class AssignmentTwo(Assignment): | ||
| 38 | example_result = 4174379265 | ||
| 39 | |||
| 40 | @classmethod | ||
| 41 | def is_invalid_id(cls, item: str) -> bool: | ||
| 42 | for i in range(1, (len(item) // 2) + 1): | ||
| 43 | pattern = item[:i] | ||
| 44 | rest = item[i:] | ||
| 45 | |||
| 46 | if re.match(rf"({pattern})+$", rest): | ||
| 47 | return True | ||
| 48 | |||
| 49 | return False | ||
| 50 | |||
| 51 | @classmethod | ||
| 52 | def find_invalid_ids(cls, item: range): | ||
| 53 | for i in item: | ||
| 54 | i_str = str(i) | ||
| 55 | |||
| 56 | if AssignmentTwo.is_invalid_id(i_str): | ||
| 57 | yield i | ||
diff --git a/day2/example.txt b/day2/example.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/day2/example.txt | |||
| @@ -0,0 +1 @@ | |||
| 11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 | |||
diff --git a/day2/test_init.py b/day2/test_init.py new file mode 100644 index 0000000..dc1ab40 --- /dev/null +++ b/day2/test_init.py | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import pytest | ||
| 3 | |||
| 4 | from day2 import AssignmentOne, AssignmentTwo | ||
| 5 | |||
| 6 | |||
| 7 | class TestAssignmentOne: | ||
| 8 | data = [ | ||
| 9 | (range(11, 23), [11, 22]), | ||
| 10 | (range(95, 116), [99]), | ||
| 11 | (range(998, 1013), [1010]), | ||
| 12 | (range(1188511880, 1188511891), [1188511885]), | ||
| 13 | (range(222220, 222225), [222222]), | ||
| 14 | (range(1698522, 1698529), []), | ||
| 15 | (range(446443, 446450), [446446]), | ||
| 16 | (range(38593856, 38593863), [38593859]), | ||
| 17 | ] | ||
| 18 | |||
| 19 | @pytest.mark.parametrize("r,invalid", data) | ||
| 20 | def test_find_invalid_ids(self, r: range, invalid: list[str]): | ||
| 21 | assert list(AssignmentOne.find_invalid_ids(r)) == invalid | ||
| 22 | |||
| 23 | |||
| 24 | class TestAssignmentTwo: | ||
| 25 | data = [ | ||
| 26 | (range(11, 23), [11, 22]), | ||
| 27 | (range(95, 116), [99, 111]), | ||
| 28 | (range(998, 1013), [999, 1010]), | ||
| 29 | (range(1188511880, 1188511891), [1188511885]), | ||
| 30 | (range(222220, 222225), [222222]), | ||
| 31 | (range(1698522, 1698529), []), | ||
| 32 | (range(446443, 446450), [446446]), | ||
| 33 | (range(38593856, 38593863), [38593859]), | ||
| 34 | (range(565653, 565660), [565656]), | ||
| 35 | (range(824824821, 824824828), [824824824]), | ||
| 36 | (range(2121212118, 2121212125), [2121212121]), | ||
| 37 | ] | ||
| 38 | |||
| 39 | @pytest.mark.parametrize("r,invalid", data) | ||
| 40 | def test_find_invalid_ids(self, r: range, invalid: list[str]): | ||
| 41 | assert list(AssignmentTwo.find_invalid_ids(r)) == invalid | ||
