diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-18 13:00:12 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-18 13:00:12 +0000 |
commit | 7d61b30fc61a0e45b0982a558122d7b3cd4548cd (patch) | |
tree | 4c2fcee92ffde8ecae42d312702aa1dd3857f3ec | |
parent | 9c579a2176bab5c4fee569bfd96323fa174e3ecd (diff) | |
download | aoc2022-7d61b30fc61a0e45b0982a558122d7b3cd4548cd.tar.gz aoc2022-7d61b30fc61a0e45b0982a558122d7b3cd4548cd.tar.xz aoc2022-7d61b30fc61a0e45b0982a558122d7b3cd4548cd.zip |
18
-rw-r--r-- | 18.py | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -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) |