-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_18.py
167 lines (150 loc) · 3.6 KB
/
day_18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import collections
import itertools
import aoc_helper
from aoc_helper import (
Grid,
PrioQueue,
SparseGrid,
decode_text,
extract_ints,
extract_iranges,
extract_ranges,
extract_uints,
frange,
irange,
iter,
list,
map,
range,
tail_call,
)
raw = aoc_helper.fetch(18, 2022)
def parse_raw(raw):
return extract_ints(raw).chunked(3)
data = parse_raw(raw)
def part_one(data):
tiles = collections.defaultdict(bool)
for pos in data:
tiles[pos] = True
count = 0
for x, y, z in data:
for dx, dy, dz in [
(-1, 0, 0),
(1, 0, 0),
(0, -1, 0),
(0, 1, 0),
(0, 0, -1),
(0, 0, 1),
]:
if not tiles[x + dx, y + dy, z + dz]:
count += 1
return count
aoc_helper.lazy_test(
day=18,
year=2022,
parse=parse_raw,
solution=part_one,
test_data=(
"""2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5""",
64,
),
)
def part_two(data):
tiles = collections.defaultdict(bool)
for pos in data:
tiles[pos] = True
count = 0
for x, y, z in data:
for dx, dy, dz in [
(-1, 0, 0),
(1, 0, 0),
(0, -1, 0),
(0, 1, 0),
(0, 0, -1),
(0, 0, 1),
]:
if not tiles[x + dx, y + dy, z + dz]:
count += 1
top = min(x for x, y, z in data)
bottom = max(x for x, y, z in data)
left = min(y for x, y, z in data)
right = max(y for x, y, z in data)
front = min(z for x, y, z in data)
back = max(z for x, y, z in data)
def is_interior_surface(tiles, x, y, z):
to_visit = collections.deque([(x, y, z)])
visited = set()
while to_visit:
x, y, z = to_visit.popleft()
if (x, y, z) in visited:
continue
visited.add((x, y, z))
if x < top or x > bottom or y < left or y > right or z < front or z > back:
return False
for dx, dy, dz in [
(-1, 0, 0),
(1, 0, 0),
(0, -1, 0),
(0, 1, 0),
(0, 0, -1),
(0, 0, 1),
]:
if (x + dx, y + dy, z + dz) not in visited and not tiles[
x + dx, y + dy, z + dz
]:
to_visit.append((x + dx, y + dy, z + dz))
return True
for (x, y, z), tile in list(tiles.items()):
if not tile:
neighbours = sum(
tiles[x + dx, y + dy, z + dz]
for dx, dy, dz in [
(-1, 0, 0),
(1, 0, 0),
(0, -1, 0),
(0, 1, 0),
(0, 0, -1),
(0, 0, 1),
]
)
interior = is_interior_surface(tiles, x, y, z)
# print(x, y, z, neighbours, interior)
if interior:
count -= neighbours
return count
aoc_helper.lazy_test(
day=18,
year=2022,
parse=parse_raw,
solution=part_two,
test_data=(
"""2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5""",
58,
),
)
aoc_helper.lazy_submit(day=18, year=2022, solution=part_one, data=data)
aoc_helper.lazy_submit(day=18, year=2022, solution=part_two, data=data)