Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Inspired from mission "calculate islands" solution in Clear category for Fused Cubes by Rcp8jzd
from collections import deque
from itertools import product
from typing import Iterable
Cube = tuple[int, int, int, int]
def neighbors(cube):
"""
Give the neighbors of a cube.
:param cube: cube coordinates, as a tuple of 3 integers:
X coordinate, Y coordinate, Z coordinate.
:return: All neighbors.
"""
x, y, z = cube
for dx, dy, dz in ((-1, 0, 0), (0, -1, 0), (0, 0, -1), (1, 0, 0),
(0, 1, 0), (0, 0, 1)):
yield x + dx, y + dy, z + dz
def fused_cubes(cubes: list[Cube]) -> Iterable[int]:
voxels = set()
for x_0, y_0, z_0, edge in cubes:
for dx, dy, dz in product(range(edge), repeat=3):
voxels.add((x_0 + dx, y_0 + dy, z_0 + dz))
# Now, similar to mission "Calculate Islands"
# https://py.checkio.org/en/mission/calculate-islands/
islands = []
while voxels:
cube = voxels.pop()
volume = 1
queue = deque([cube])
while queue:
current = queue.popleft()
for n in neighbors(current):
if n in voxels:
voxels.remove(n)
volume += 1
queue.append(n)
islands.append(volume)
islands.sort()
return islands
Dec. 9, 2020
Comments: