From 7d61b30fc61a0e45b0982a558122d7b3cd4548cd Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 18 Dec 2022 13:00:12 +0000 Subject: 18 --- 18.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 18.py 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) -- cgit v1.2.3-54-g00ecf