summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-04 14:08:15 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-04 14:08:15 +0100
commit87f25f31adcc1a90bd99d40c5790290ecbcd7ce0 (patch)
tree432120b8f56574bb4b1437ab2c1f077ed4a8eddd
parent64a9220b62976636f012e4b1e2e6ac8cbde9827e (diff)
download2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.tar.gz
2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.tar.bz2
2022-87f25f31adcc1a90bd99d40c5790290ecbcd7ce0.zip
Day 4
-rw-r--r--day4/__init__.py68
-rw-r--r--day4/example.txt6
-rw-r--r--day4/input.txt1000
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 -*-
2from abc import ABC
3from dataclasses import dataclass
4from typing import Tuple, Iterator, Any
5
6from aoc import BaseAssignment
7
8
9@dataclass
10class 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
26class 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
55class 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
64class 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 @@
12-4,6-8
22-3,4-5
35-7,7-9
42-8,3-7
56-6,4-6
62-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 @@
17-96,6-95
236-41,35-40
335-84,83-91
495-96,5-95
53-3,4-99
612-21,21-22
710-28,10-28
86-11,10-12
912-91,76-98
1017-69,18-69
1126-81,7-27
1226-42,57-80
1332-38,33-38
144-83,5-83
1533-85,34-86
1614-95,6-78
1779-84,78-87
1812-61,11-62
1956-92,55-92
2034-63,9-34
2133-44,33-85
225-17,16-26
2321-43,21-32
2412-60,59-60
2510-48,10-49
2694-94,71-94
2745-46,24-46
2881-97,82-98
295-27,27-57
303-96,2-22
315-92,2-97
3253-87,76-90
3368-83,82-82
3455-67,55-73
3539-79,78-78
3681-86,87-97
373-25,2-67
3850-63,62-62
395-96,5-97
4027-88,28-88
4119-53,20-53
4255-89,55-90
4343-46,43-99
4482-95,56-96
458-57,8-8
4690-95,2-90
4756-78,34-81
4853-81,54-81
4920-30,9-62
5012-83,11-16
5164-65,63-69
5215-95,15-94
536-94,4-93
5436-93,8-92
5567-74,68-74
564-89,5-89
5723-24,22-23
5844-53,45-80
5943-60,43-83
6061-69,62-78
6177-77,25-77
6243-65,43-66
6312-65,13-84
6454-56,54-97
6513-34,33-60
6620-55,55-56
6734-93,35-92
6826-85,27-96
6951-97,9-97
7018-41,17-40
713-14,14-80
7255-66,8-56
734-89,64-98
7420-98,20-63
751-16,15-22
7620-93,41-92
779-86,8-10
785-27,27-28
7929-51,30-48
8044-64,44-44
8129-96,2-96
821-98,13-97
836-84,18-84
8428-81,27-72
857-95,6-95
8643-84,78-84
872-62,35-98
8846-88,9-88
8911-19,10-18
9021-81,22-89
9198-99,78-98
9226-57,25-57
9312-31,10-30
9431-84,32-85
9593-93,91-93
9610-89,8-76
976-36,7-36
9851-96,95-99
9914-32,5-17
10023-37,34-38
10114-15,22-82
10295-96,72-95
10338-52,53-53
10425-94,25-96
10538-74,37-74
10673-94,41-73
10772-85,35-84
10815-98,14-16
10981-94,82-94
11085-89,85-87
1113-87,3-83
11284-93,4-91
11342-57,42-50
11442-80,23-42
11518-26,18-27
11644-58,44-99
11735-68,3-67
11840-97,41-53
11911-99,12-12
1207-51,12-59
12113-71,68-71
1225-12,4-97
1238-20,20-79
12413-91,12-14
12533-62,18-32
1263-94,5-94
12726-98,26-93
12831-38,30-85
12913-83,14-82
1306-74,73-74
13173-73,45-62
13238-38,38-38
1339-21,4-20
13414-60,13-83
13583-90,83-89
13673-73,6-74
1371-3,3-94
1383-84,3-85
13961-90,81-97
14093-93,23-94
14131-81,81-81
14226-76,75-75
14395-96,94-94
14470-72,71-89
14595-95,4-95
14610-70,9-11
1473-99,4-98
14826-99,26-92
14977-95,78-90
15047-82,48-83
1514-83,82-88
15251-51,42-50
15310-86,11-96
1549-46,39-49
15520-78,20-79
1561-62,2-61
15757-59,58-75
1584-90,28-90
1594-4,4-52
16034-81,35-96
16132-68,67-83
16283-87,84-88
16371-96,71-71
16461-96,95-99
1653-99,1-4
16613-94,12-95
16713-86,14-95
16836-89,35-88
1698-12,12-96
17034-55,33-55
17143-55,56-95
17238-39,7-38
1733-48,47-98
17486-86,5-86
1754-64,4-4