summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--18.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/18.py b/18.py
new file mode 100644
index 0000000..047a942
--- /dev/null
+++ b/18.py
@@ -0,0 +1,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)