diff options
| author | 2022-12-04 14:08:15 +0100 | |
|---|---|---|
| committer | 2022-12-04 14:08:15 +0100 | |
| commit | 87f25f31adcc1a90bd99d40c5790290ecbcd7ce0 (patch) | |
| tree | 432120b8f56574bb4b1437ab2c1f077ed4a8eddd /day4/__init__.py | |
| parent | 64a9220b62976636f012e4b1e2e6ac8cbde9827e (diff) | |
| download | 2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.tar.gz 2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.tar.bz2 2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.zip | |
Day 4
Diffstat (limited to 'day4/__init__.py')
| -rw-r--r-- | day4/__init__.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/day4/__init__.py b/day4/__init__.py new file mode 100644 index 0000000..b917a9b --- /dev/null +++ b/day4/__init__.py | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from dataclasses import dataclass | ||
| 4 | from typing import Tuple, Iterator, Any | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | |||
| 8 | |||
| 9 | @dataclass | ||
| 10 | class Section: | ||
| 11 | start: int | ||
| 12 | end: int | ||
| 13 | |||
| 14 | def fully_contains(self, other: "Section") -> bool: | ||
| 15 | return self.start <= other.start and self.end >= other.end | ||
| 16 | |||
| 17 | def overlaps(self, other: "Section") -> bool: | ||
| 18 | return ( | ||
| 19 | self.start <= other.start <= self.end | ||
| 20 | or self.start <= other.end <= self.end | ||
| 21 | or self.fully_contains(other) | ||
| 22 | or other.fully_contains(self) | ||
| 23 | ) | ||
| 24 | |||
| 25 | |||
| 26 | class Assignment(BaseAssignment, ABC): | ||
| 27 | def parse_section(self, section: str) -> Section: | ||
| 28 | start, end = section.split("-") | ||
| 29 | |||
| 30 | return Section( | ||
| 31 | start=int(start), | ||
| 32 | end=int(end), | ||
| 33 | ) | ||
| 34 | |||
| 35 | def parse_sections(self, sections: str) -> Tuple[Section, Section]: | ||
| 36 | section_1, section_2 = sections.split(",") | ||
| 37 | |||
| 38 | return (self.parse_section(section_1), self.parse_section(section_2)) | ||
| 39 | |||
| 40 | def check(self, section_1: Section, section_2: Section) -> bool: | ||
| 41 | raise NotImplementedError() | ||
| 42 | |||
| 43 | def run(self, input: Iterator) -> Any: | ||
| 44 | overlapping = [] | ||
| 45 | |||
| 46 | for sections in input: | ||
| 47 | section_1, section_2 = self.parse_sections(sections) | ||
| 48 | |||
| 49 | if self.check(section_1, section_2): | ||
| 50 | overlapping.append((section_1, section_2)) | ||
| 51 | |||
| 52 | return len(overlapping) | ||
| 53 | |||
| 54 | |||
| 55 | class AssignmentOne(Assignment): | ||
| 56 | example_result = 2 | ||
| 57 | |||
| 58 | def check(self, section_1: Section, section_2: Section) -> bool: | ||
| 59 | return section_1.fully_contains(section_2) or section_2.fully_contains( | ||
| 60 | section_1 | ||
| 61 | ) | ||
| 62 | |||
| 63 | |||
| 64 | class AssignmentTwo(Assignment): | ||
| 65 | example_result = 4 | ||
| 66 | |||
| 67 | def check(self, section_1: Section, section_2: Section) -> bool: | ||
| 68 | return section_1.overlaps(section_2) | ||
