diff options
| -rw-r--r-- | day4/__init__.py | 68 | ||||
| -rw-r--r-- | day4/example.txt | 6 | ||||
| -rw-r--r-- | day4/input.txt | 1000 |
3 files changed, 1074 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) | ||
diff --git a/day4/example.txt b/day4/example.txt new file mode 100644 index 0000000..9f9e9cf --- /dev/null +++ b/day4/example.txt | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | 2-4,6-8 | ||
| 2 | 2-3,4-5 | ||
| 3 | 5-7,7-9 | ||
| 4 | 2-8,3-7 | ||
| 5 | 6-6,4-6 | ||
| 6 | 2-6,4-8 | ||
diff --git a/day4/input.txt b/day4/input.txt new file mode 100644 index 0000000..c84044f --- /dev/null +++ b/day4/input.txt | |||
| @@ -0,0 +1,1000 @@ | |||
| 1 | 7-96,6-95 | ||
| 2 | 36-41,35-40 | ||
| 3 | 35-84,83-91 | ||
| 4 | 95-96,5-95 | ||
| 5 | 3-3,4-99 | ||
| 6 | 12-21,21-22 | ||
| 7 | 10-28,10-28 | ||
| 8 | 6-11,10-12 | ||
| 9 | 12-91,76-98 | ||
| 10 | 17-69,18-69 | ||
| 11 | 26-81,7-27 | ||
| 12 | 26-42,57-80 | ||
| 13 | 32-38,33-38 | ||
| 14 | 4-83,5-83 | ||
| 15 | 33-85,34-86 | ||
| 16 | 14-95,6-78 | ||
| 17 | 79-84,78-87 | ||
| 18 | 12-61,11-62 | ||
| 19 | 56-92,55-92 | ||
| 20 | 34-63,9-34 | ||
| 21 | 33-44,33-85 | ||
| 22 | 5-17,16-26 | ||
| 23 | 21-43,21-32 | ||
| 24 | 12-60,59-60 | ||
| 25 | 10-48,10-49 | ||
| 26 | 94-94,71-94 | ||
| 27 | 45-46,24-46 | ||
| 28 | 81-97,82-98 | ||
| 29 | 5-27,27-57 | ||
| 30 | 3-96,2-22 | ||
| 31 | 5-92,2-97 | ||
| 32 | 53-87,76-90 | ||
| 33 | 68-83,82-82 | ||
| 34 | 55-67,55-73 | ||
| 35 | 39-79,78-78 | ||
| 36 | 81-86,87-97 | ||
| 37 | 3-25,2-67 | ||
| 38 | 50-63,62-62 | ||
| 39 | 5-96,5-97 | ||
| 40 | 27-88,28-88 | ||
| 41 | 19-53,20-53 | ||
| 42 | 55-89,55-90 | ||
| 43 | 43-46,43-99 | ||
| 44 | 82-95,56-96 | ||
| 45 | 8-57,8-8 | ||
| 46 | 90-95,2-90 | ||
| 47 | 56-78,34-81 | ||
| 48 | 53-81,54-81 | ||
| 49 | 20-30,9-62 | ||
| 50 | 12-83,11-16 | ||
| 51 | 64-65,63-69 | ||
| 52 | 15-95,15-94 | ||
| 53 | 6-94,4-93 | ||
| 54 | 36-93,8-92 | ||
| 55 | 67-74,68-74 | ||
| 56 | 4-89,5-89 | ||
| 57 | 23-24,22-23 | ||
| 58 | 44-53,45-80 | ||
| 59 | 43-60,43-83 | ||
| 60 | 61-69,62-78 | ||
| 61 | 77-77,25-77 | ||
| 62 | 43-65,43-66 | ||
| 63 | 12-65,13-84 | ||
| 64 | 54-56,54-97 | ||
| 65 | 13-34,33-60 | ||
| 66 | 20-55,55-56 | ||
| 67 | 34-93,35-92 | ||
| 68 | 26-85,27-96 | ||
| 69 | 51-97,9-97 | ||
| 70 | 18-41,17-40 | ||
| 71 | 3-14,14-80 | ||
| 72 | 55-66,8-56 | ||
| 73 | 4-89,64-98 | ||
| 74 | 20-98,20-63 | ||
| 75 | 1-16,15-22 | ||
| 76 | 20-93,41-92 | ||
| 77 | 9-86,8-10 | ||
| 78 | 5-27,27-28 | ||
| 79 | 29-51,30-48 | ||
| 80 | 44-64,44-44 | ||
| 81 | 29-96,2-96 | ||
| 82 | 1-98,13-97 | ||
| 83 | 6-84,18-84 | ||
| 84 | 28-81,27-72 | ||
| 85 | 7-95,6-95 | ||
| 86 | 43-84,78-84 | ||
| 87 | 2-62,35-98 | ||
| 88 | 46-88,9-88 | ||
| 89 | 11-19,10-18 | ||
| 90 | 21-81,22-89 | ||
| 91 | 98-99,78-98 | ||
| 92 | 26-57,25-57 | ||
| 93 | 12-31,10-30 | ||
| 94 | 31-84,32-85 | ||
| 95 | 93-93,91-93 | ||
| 96 | 10-89,8-76 | ||
| 97 | 6-36,7-36 | ||
| 98 | 51-96,95-99 | ||
| 99 | 14-32,5-17 | ||
| 100 | 23-37,34-38 | ||
| 101 | 14-15,22-82 | ||
| 102 | 95-96,72-95 | ||
| 103 | 38-52,53-53 | ||
| 104 | 25-94,25-96 | ||
| 105 | 38-74,37-74 | ||
| 106 | 73-94,41-73 | ||
| 107 | 72-85,35-84 | ||
| 108 | 15-98,14-16 | ||
| 109 | 81-94,82-94 | ||
| 110 | 85-89,85-87 | ||
| 111 | 3-87,3-83 | ||
| 112 | 84-93,4-91 | ||
| 113 | 42-57,42-50 | ||
| 114 | 42-80,23-42 | ||
| 115 | 18-26,18-27 | ||
| 116 | 44-58,44-99 | ||
| 117 | 35-68,3-67 | ||
| 118 | 40-97,41-53 | ||
| 119 | 11-99,12-12 | ||
| 120 | 7-51,12-59 | ||
| 121 | 13-71,68-71 | ||
| 122 | 5-12,4-97 | ||
| 123 | 8-20,20-79 | ||
| 124 | 13-91,12-14 | ||
| 125 | 33-62,18-32 | ||
| 126 | 3-94,5-94 | ||
| 127 | 26-98,26-93 | ||
| 128 | 31-38,30-85 | ||
| 129 | 13-83,14-82 | ||
| 130 | 6-74,73-74 | ||
| 131 | 73-73,45-62 | ||
| 132 | 38-38,38-38 | ||
| 133 | 9-21,4-20 | ||
| 134 | 14-60,13-83 | ||
| 135 | 83-90,83-89 | ||
| 136 | 73-73,6-74 | ||
| 137 | 1-3,3-94 | ||
| 138 | 3-84,3-85 | ||
| 139 | 61-90,81-97 | ||
| 140 | 93-93,23-94 | ||
| 141 | 31-81,81-81 | ||
| 142 | 26-76,75-75 | ||
| 143 | 95-96,94-94 | ||
| 144 | 70-72,71-89 | ||
| 145 | 95-95,4-95 | ||
| 146 | 10-70,9-11 | ||
| 147 | 3-99,4-98 | ||
| 148 | 26-99,26-92 | ||
| 149 | 77-95,78-90 | ||
| 150 | 47-82,48-83 | ||
| 151 | 4-83,82-88 | ||
| 152 | 51-51,42-50 | ||
| 153 | 10-86,11-96 | ||
| 154 | 9-46,39-49 | ||
| 155 | 20-78,20-79 | ||
| 156 | 1-62,2-61 | ||
| 157 | 57-59,58-75 | ||
| 158 | 4-90,28-90 | ||
| 159 | 4-4,4-52 | ||
| 160 | 34-81,35-96 | ||
| 161 | 32-68,67-83 | ||
| 162 | 83-87,84-88 | ||
| 163 | 71-96,71-71 | ||
| 164 | 61-96,95-99 | ||
| 165 | 3-99,1-4 | ||
| 166 | 13-94,12-95 | ||
| 167 | 13-86,14-95 | ||
| 168 | 36-89,35-88 | ||
| 169 | 8-12,12-96 | ||
| 170 | 34-55,33-55 | ||
| 171 | 43-55,56-95 | ||
| 172 | 38-39,7-38 | ||
| 173 | 3-48,47-98 | ||
| 174 | 86-86,5-86 | ||
| 175 | 4-64,4-4 | ||
| 176 | 80-97,41-98 | ||
| 177 | 16-16,30-54 | ||
| 178 | 3-94,4-94 | ||
| 179 | 16-90,15-91 | ||
| 180 | 7-19,7-84 | ||
| 181 | 64-79,8-79 | ||
| 182 | 29-94,94-95 | ||
| 183 | 25-86,24-85 | ||
| 184 | 91-95,90-93 | ||
