summaryrefslogtreecommitdiffstats
path: root/day18
diff options
context:
space:
mode:
Diffstat (limited to 'day18')
-rw-r--r--day18/__init__.py75
-rw-r--r--day18/example.txt13
-rw-r--r--day18/input.txt2057
-rw-r--r--day18/test_init.py24
4 files changed, 2169 insertions, 0 deletions
diff --git a/day18/__init__.py b/day18/__init__.py
new file mode 100644
index 0000000..0500963
--- /dev/null
+++ b/day18/__init__.py
@@ -0,0 +1,75 @@
1# -*- coding: utf-8 -*-
2from abc import ABC
3from typing import Iterator, Set
4
5from aoc import BaseAssignment
6from aoc.datastructures import Coordinate3
7from aoc.mixins import BreathFirstSearchMixin
8
9
10class Assignment(BaseAssignment[int, Coordinate3], ABC):
11 def parse_item(self, item: str) -> Coordinate3:
12 x, y, z = item.split(",")
13 return Coordinate3(int(x), int(y), int(z))
14
15 @staticmethod
16 def open_sides(blocks: Set[Coordinate3]) -> int:
17 return len(
18 [
19 neighbour
20 for block in blocks
21 for neighbour in block.neighbours(True)
22 if neighbour not in blocks
23 ]
24 )
25
26
27class AssignmentOne(Assignment):
28 example_result = 64
29
30 def run(self, input: Iterator[Coordinate3]) -> int:
31 blocks = set(input)
32 return self.open_sides(blocks)
33
34
35class AssignmentTwo(Assignment, BreathFirstSearchMixin[Coordinate3]):
36 example_result = 58
37
38 def run(self, input: Iterator[Coordinate3]) -> int:
39 blocks = set(input)
40
41 lower_x = min([block.x for block in blocks]) - 1
42 higher_x = max([block.x for block in blocks]) + 1
43 lower_y = min([block.y for block in blocks]) - 1
44 higher_y = max([block.y for block in blocks]) + 1
45 lower_z = min([block.z for block in blocks]) - 1
46 higher_z = max([block.z for block in blocks]) + 1
47
48 all_coordinates = set(
49 Coordinate3(x, y, z)
50 for x in range(lower_x, higher_x + 1)
51 for y in range(lower_y, higher_y + 1)
52 for z in range(lower_z, higher_z + 1)
53 )
54
55 def nb(c):
56 for n in c.neighbours(True):
57 if n in all_coordinates and n not in blocks:
58 yield n
59
60 outer_coordinates = {
61 coordinate
62 for coordinate in self.bfs(
63 Coordinate3(lower_x, lower_y, lower_z),
64 neighbours=nb,
65 )
66 }
67
68 return len(
69 [
70 neighbour
71 for block in blocks
72 for neighbour in block.neighbours(True)
73 if neighbour in outer_coordinates
74 ]
75 )
diff --git a/day18/example.txt b/day18/example.txt
new file mode 100644
index 0000000..73a7202
--- /dev/null
+++ b/day18/example.txt
@@ -0,0 +1,13 @@
12,2,2
21,2,2
33,2,2
42,1,2
52,3,2
62,2,1
72,2,3
82,2,4
92,2,6
101,2,5
113,2,5
122,1,5
132,3,5
diff --git a/day18/input.txt b/day18/input.txt
new file mode 100644
index 0000000..acf8240
--- /dev/null
+++ b/day18/input.txt
@@ -0,0 +1,2057 @@
14,15,12
216,10,16
315,15,10
414,11,16
55,7,15
615,9,12
78,5,3
82,9,10
913,16,9
102,7,9
116,5,5
1217,7,10
136,13,15
143,14,13
154,14,13
163,6,7
175,16,7
186,16,12
1916,13,6
204,10,4
213,5,8
223,14,9
2314,9,1
2416,5,9
2516,12,15
2616,7,6
274,4,9
2817,13,10
2913,4,14
309,2,10
315,4,12
326,9,16
3310,2,13
3418,10,8
356,4,4
365,14,5
377,9,18
387,14,16
397,4,15
409,11,17
4112,10,16
422,6,8
435,13,4
4412,2,9
4514,4,16
469,8,4
474,10,3
485,14,4
4913,16,13
5011,8,17
5116,8,15
523,12,12
5313,13,16
5415,7,6
5513,16,7
562,10,9
573,11,5
583,9,4
5911,15,14
6013,6,13
614,12,4
626,2,9
635,13,9
643,7,7
6514,8,16
665,16,11
677,16,13
687,6,3
698,4,4
7014,16,8
7118,7,9
727,4,14
7314,11,4
747,9,3
7512,8,1
7611,8,1
776,15,7
7811,2,12
791,7,9
8011,10,17
812,11,6
8213,10,3
8316,13,12
845,8,3
858,5,2
8611,7,2
877,18,12
8814,7,3
895,10,16
9016,7,12
919,7,1
929,16,7
936,9,17
9416,11,8
9514,12,4
9611,12,3
977,17,12
9811,3,4
995,5,14
1003,13,8
1017,17,8
1024,10,12
10316,15,8
10416,11,15
1053,3,9
10615,6,9
10712,4,7
10816,10,14
10916,9,5
1107,12,17
11117,13,9
1122,10,8
1135,3,10
11417,8,5
11512,17,13
1169,15,12
1178,6,16
1188,14,3
1197,2,12
12017,9,6
1217,11,17
12212,5,3
12315,16,9
1248,10,0
12514,9,17
1263,11,9
12715,7,15
1288,16,14
1293,12,15
13015,14,13
1312,9,12
13214,3,7
1339,4,5
13412,2,6
13512,16,11
13613,2,9
1377,15,14
1387,5,3
13917,7,7
1406,8,17
14110,1,9
14210,4,5
1434,17,10
14410,18,10
1458,7,16
1465,2,7
1477,11,2
1482,11,11
14910,3,15
1509,15,14
1519,4,4
1529,11,1
1538,12,16
1547,8,1
15510,3,5
15614,16,10
1574,7,16
1587,12,3
15916,13,14
1604,14,10
16113,2,7
1628,18,7
16310,15,3
1646,5,14
16518,11,12
1664,15,13
16715,5,14
16814,15,14