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
|
from utils import open_day
with open_day(18) as f:
points = {tuple(map(int, line.rstrip().split(','))) for line in f}
def neighbours(p):
yield (p[0] + 1, p[1] , p[2] )
yield (p[0] - 1, p[1] , p[2] )
yield (p[0] , p[1] + 1, p[2] )
yield (p[0] , p[1] - 1, p[2] )
yield (p[0] , p[1] , p[2] + 1)
yield (p[0] , p[1] , p[2] - 1)
p1 = 0
for point in points:
for n in neighbours(point):
if n not in points:
p1 += 1
print(p1)
it = iter(points)
minx, miny, minz = next(it)
maxx, maxy, maxz = minx, miny, minz
for x, y, z in it:
if x < minx: minx = x
elif x > maxx: maxx = x
if y < miny: miny = y
elif y > maxy: maxy = y
if z < minz: minz = z
elif z > maxz: maxz = z
p2 = 0
to_visit = {(minx - 1, miny - 1, minz - 1)}
visited = set()
while to_visit:
p = to_visit.pop()
visited.add(p)
for n in neighbours(p):
if not (minx - 1 <= n[0] <= maxx + 1): continue
if not (miny - 1 <= n[1] <= maxy + 1): continue
if not (minz - 1 <= n[2] <= maxz + 1): continue
if n in visited: continue
if n in points: p2 += 1
else: to_visit.add(n)
print(p2)
|