Skip to content

Commit

Permalink
Day 18, not too neat, part 2
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Grant <rob.grant@nanoporetech.com>
  • Loading branch information
robertlagrant committed Dec 21, 2022
1 parent c197647 commit bb439fc
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions day18/run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
from itertools import combinations_with_replacement

from inputs import REAL as data

points = set(tuple(map(int, line.split(","))) for line in data.split("\n"))

MANHATTAN_3D = ((1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1))

CUBES = set(tuple(map(int, line.split(","))) for line in data.split("\n"))


def face_count(c, cubes):
return sum((c[0] + xd, c[1] + yd, c[2] + zd) not in cubes for xd, yd, zd in MANHATTAN_3D)


def face_count(p, points):
return sum((p[0] + xd, p[1] + yd, p[2] + zd) not in points for xd, yd, zd in ((1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)))
def face_count_in_steam(c, steam):
return sum((c[0] + xd, c[1] + yd, c[2] + zd) in steam for xd, yd, zd in MANHATTAN_3D)


def fill_with_steam(cubes):
cubes_list = list(cubes)
x_min, y_min, z_min = cubes_list[0]
x_max, y_max, z_max = cubes_list[0]

for c in cubes_list[1:]:
x_min, x_max = min(x_min, c[0]), max(x_max, c[0])
y_min, y_max = min(y_min, c[1]), max(y_max, c[1])
z_min, z_max = min(z_min, c[2]), max(z_max, c[2])

steam = set()
cubes_touching_steam = set()

candidate_steam = [(x_min - 1, y_min - 1, z_min - 1)]
while candidate_steam:
s = candidate_steam.pop()
if s not in steam:
steam.add(s)
for dx, dy, dz in MANHATTAN_3D:
m = s[0] + dx, s[1] + dy, s[2] + dz
if (x_min - 1) <= m[0] <= (x_max + 1) and (y_min - 1) <= m[1] <= (y_max + 1) and (z_min - 1) <= m[2] <= (z_max + 1):
if m in cubes:
cubes_touching_steam.add(m)
if m not in steam and m not in cubes:
candidate_steam.append(m)

return cubes_touching_steam, steam

cubes_touching_steam, steam = fill_with_steam(CUBES)

print(f"Part 1: {sum(face_count(p, points) for p in points)}")
print(f"Part 1: {sum(face_count(c, CUBES) for c in CUBES)}")
print(f"Part 2: {sum(face_count_in_steam(c, steam) for c in cubes_touching_steam)}")

0 comments on commit bb439fc

Please sign in to comment.