Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Fused Cubes by sawako.oono
from typing import Tuple, List, Iterable
from itertools import chain
def fused_cubes(cubes: List[Tuple[int]])->Iterable[int]:
points=[]
res=[]
for p in cubes:
points.append(set([(x,y,z) for x in range(p[0],p[0]+p[3]) for y in range(p[1],p[1]+p[3]) for z in range(p[2],p[2]+p[3])]))
for i in range(len(cubes)):
c=cubes[i]
cx,cy,cz,cl=c[0],c[1],c[2],c[3]
for j in range(len(cubes)):
if i>=j:
continue
d=cubes[j]
dx,dy,dz,dl=d[0],d[1],d[2],d[3]
counter=sum((cx-dl share part
counter==2 and counter2==3 -> touch
counter==1 and counter2==3 -> share edge
counter==0 and counter2==3 -> share apex
Only if "share part" or "touch" two cubes are considered as one object.
"""
if counter==3 or (counter==2 and counter2==3):
if res==[]:
res.append([{i,j},points[i]|points[j]])
else:
done=0
for m in range(len(res)):
if done==1:break
if i in res[m][0]:
if j in res[m][0]:
break
key=res[m][0].copy()
new_k=key.copy()
ps=res[m][1].copy()
new_p=ps|points[i]|points[j]
new_k.add(j)
res.pop(m)
res.insert(m,[new_k,new_p])
for n in range(len(res)):
if j in res[n][0] and m!=n:
add_key=res[n][0].copy()
add_p=res[n][1].copy()
new_k2=new_k|add_key
new_p2=new_p|add_p
res.pop(n)
res.insert(n,[new_k2,new_p2])
res.pop(m)
done=1
break
else:
if i not in [y for y in chain.from_iterable([x[0] for x in res])]:
res.append([{i},points[i]])
if j not in [y for y in chain.from_iterable([x[0] for x in res])]:
res.append([{j},points[j]])
return [len(x[1]) for x in res]
if __name__ == '__main__':
assert sorted(fused_cubes([(0, 0, 0, 3), (1, 2, 2, 3)])) == [52], 'fused'
assert sorted(fused_cubes([(0, 0, 0, 3), (1, 3, 2, 3)])) == [54], 'touch with faces'
assert sorted(fused_cubes([(0, 0, 0, 3), (1, 3, 3, 3)])) == [27, 27], 'touch with edges'
assert sorted(fused_cubes([(0, 0, 0, 3), (3, 3, 3, 3)])) == [27, 27], 'touch with vertices'
assert sorted(fused_cubes([(0, 0, 0, 3), (3, 4, 3, 3)])) == [27, 27], 'separated'
assert sorted(fused_cubes([(0, 0, 0, 3), (-2, -2, -2, 3)])) == [53], 'negative coordinates'
print("Coding complete? Click 'Check' to earn cool rewards!")
July 4, 2021
Comments: