diff options
| author | 2022-12-19 09:30:43 +0100 | |
|---|---|---|
| committer | 2022-12-19 09:30:43 +0100 | |
| commit | f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2 (patch) | |
| tree | 535a7048c7547e1eaef6f97dcfd8b2cad598ca50 /day18 | |
| parent | 06cb539f69f0b501afaa9ef5b6d89863e1c9d111 (diff) | |
| download | 2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.gz 2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.bz2 2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.zip | |
Day 18
Diffstat (limited to 'day18')
| -rw-r--r-- | day18/__init__.py | 75 | ||||
| -rw-r--r-- | day18/example.txt | 13 | ||||
| -rw-r--r-- | day18/input.txt | 2057 | ||||
| -rw-r--r-- | day18/test_init.py | 24 |
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 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from typing import Iterator, Set | ||
| 4 | |||
| 5 | from aoc import BaseAssignment | ||
| 6 | from aoc.datastructures import Coordinate3 | ||
| 7 | from aoc.mixins import BreathFirstSearchMixin | ||
| 8 | |||
| 9 | |||
| 10 | class 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 | |||
| 27 | class 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 | |||
| 35 | class 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 @@ | |||
| 1 | 2,2,2 | ||
| 2 | 1,2,2 | ||
| 3 | 3,2,2 | ||
| 4 | 2,1,2 | ||
| 5 | 2,3,2 | ||
| 6 | 2,2,1 | ||
| 7 | 2,2,3 | ||
| 8 | 2,2,4 | ||
| 9 | 2,2,6 | ||
| 10 | 1,2,5 | ||
| 11 | 3,2,5 | ||
| 12 | 2,1,5 | ||
| 13 | 2,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 @@ | |||
| 1 | 4,15,12 | ||
| 2 | 16,10,16 | ||
| 3 | 15,15,10 | ||
| 4 | 14,11,16 | ||
| 5 | 5,7,15 | ||
| 6 | 15,9,12 | ||
| 7 | 8,5,3 | ||
| 8 | 2,9,10 | ||
| 9 | 13,16,9 | ||
| 10 | 2,7,9 | ||
| 11 | 6,5,5 | ||
| 12 | 17,7,10 | ||
| 13 | 6,13,15 | ||
| 14 | 3,14,13 | ||
| 15 | 4,14,13 | ||
| 16 | 3,6,7 | ||
| 17 | 5,16,7 | ||
| 18 | 6,16,12 | ||
| 19 | 16,13,6 | ||
| 20 | 4,10,4 | ||
| 21 | 3,5,8 | ||
| 22 | 3,14,9 | ||
| 23 | 14,9,1 | ||
| 24 | 16,5,9 | ||
| 25 | 16,12,15 | ||
| 26 | 16,7,6 | ||
| 27 | 4,4,9 | ||
| 28 | 17,13,10 | ||
| 29 | 13,4,14 | ||
| 30 | 9,2,10 | ||
| 31 | 5,4,12 | ||
| 32 | 6,9,16 | ||
| 33 | 10,2,13 | ||
| 34 | 18,10,8 | ||
| 35 | 6,4,4 | ||
| 36 | 5,14,5 | ||
| 37 | 7,9,18 | ||
| 38 | 7,14,16 | ||
| 39 | 7,4,15 | ||
| 40 | 9,11,17 | ||
| 41 | 12,10,16 | ||
| 42 | 2,6,8 | ||
| 43 | 5,13,4 | ||
| 44 | 12,2,9 | ||
| 45 | 14,4,16 | ||
| 46 | 9,8,4 | ||
| 47 | 4,10,3 | ||
| 48 | 5,14,4 | ||
| 49 | 13,16,13 | ||
| 50 | 11,8,17 | ||
| 51 | 16,8,15 | ||
| 52 | 3,12,12 | ||
| 53 | 13,13,16 | ||
| 54 | 15,7,6 | ||
| 55 | 13,16,7 | ||
| 56 | 2,10,9 | ||
| 57 | 3,11,5 | ||
| 58 | 3,9,4 | ||
| 59 | 11,15,14 | ||
| 60 | 13,6,13 | ||
| 61 | 4,12,4 | ||
| 62 | 6,2,9 | ||
| 63 | 5,13,9 | ||
| 64 | 3,7,7 | ||
| 65 | 14,8,16 | ||
| 66 | 5,16,11 | ||
| 67 | 7,16,13 | ||
| 68 | 7,6,3 | ||
| 69 | 8,4,4 | ||
| 70 | 14,16,8 | ||
| 71 | 18,7,9 | ||
| 72 | 7,4,14 | ||
| 73 | 14,11,4 | ||
| 74 | 7,9,3 | ||
| 75 | 12,8,1 | ||
| 76 | 11,8,1 | ||
| 77 | 6,15,7 | ||
| 78 | 11,2,12 | ||
| 79 | 1,7,9 | ||
| 80 | 11,10,17 | ||
| 81 | 2,11,6 | ||
| 82 | 13,10,3 | ||
| 83 | 16,13,12 | ||
| 84 | 5,8,3 | ||
| 85 | 8,5,2 | ||
| 86 | 11,7,2 | ||
| 87 | 7,18,12 | ||
| 88 | 14,7,3 | ||
| 89 | 5,10,16 | ||
| 90 | 16,7,12 | ||
| 91 | 9,7,1 | ||
| 92 | 9,16,7 | ||
| 93 | 6,9,17 | ||
| 94 | 16,11,8 | ||
| 95 | 14,12,4 | ||
| 96 | 11,12,3 | ||
| 97 | 7,17,12 | ||
| 98 | 11,3,4 | ||
| 99 | 5,5,14 | ||
| 100 | 3,13,8 | ||
| 101 | 7,17,8 | ||
| 102 | 4,10,12 | ||
| 103 | 16,15,8 | ||
| 104 | 16,11,15 | ||
| 105 | 3,3,9 | ||
| 106 | 15,6,9 | ||
| 107 | 12,4,7 | ||
| 108 | 16,10,14 | ||
| 109 | 16,9,5 | ||
| 110 | 7,12,17 | ||
| 111 | 17,13,9 | ||
| 112 | 2,10,8 | ||
| 113 | 5,3,10 | ||
| 114 | 17,8,5 | ||
| 115 | 12,17,13 | ||
| 116 | 9,15,12 | ||
| 117 | 8,6,16 | ||
| 118 | 8,14,3 | ||
| 119 | 7,2,12 | ||
| 120 | 17,9,6 | ||
| 121 | 7,11,17 | ||
| 122 | 12,5,3 | ||
| 123 | 15,16,9 | ||
| 124 | 8,10,0 | ||
| 125 | 14,9,17 | ||
| 126 | 3,11,9 | ||
| 127 | 15,7,15 | ||
| 128 | 8,16,14 | ||
| 129 | 3,12,15 | ||
| 130 | 15,14,13 | ||
| 131 | 2,9,12 | ||
| 132 | 14,3,7 | ||
| 133 | 9,4,5 | ||
| 134 | 12,2,6 | ||
| 135 | 12,16,11 | ||
| 136 | 13,2,9 | ||
| 137 | 7,15,14 | ||
| 138 | 7,5,3 | ||
| 139 | 17,7,7 | ||
| 140 | 6,8,17 | ||
| 141 | 10,1,9 | ||
| 142 | 10,4,5 | ||
| 143 | 4,17,10 | ||
| 144 | 10,18,10 | ||
| 145 | 8,7,16 | ||
| 146 | 5,2,7 | ||
| 147 | 7,11,2 | ||
| 148 | 2,11,11 | ||
| 149 | 10,3,15 | ||
| 150 | 9,15,14 | ||
| 151 | 9,4,4 | ||
| 152 | 9,11,1 | ||
| 153 | 8,12,16 | ||
| 154 | 7,8,1 | ||
| 155 | 10,3,5 | ||
| 156 | 14,16,10 | ||
| 157 | 4,7,16 | ||
| 158 | 7,12,3 | ||
