"""Check both cube lists and draw the carvings and their common shadows."""
from collections import deque
from itertools import product
from pathlib import Path
from xml.sax.saxutils import escape
HERE = Path(__file__).resolve().parent
GRID = set(product(range(10), repeat=3))
DIRS = ((1,0,0),(-1,0,0),(0,1,0),(0,-1,0),(0,0,1),(0,0,-1))
def cubes(name):
rows = [tuple(map(int, line.split())) for line in (HERE/name).read_text().splitlines()]
assert len(rows) == len(set(rows)) and set(rows) <= GRID
return set(rows)
def add(p, q):
return tuple(a+b for a,b in zip(p,q))
def connected(block):
seen = {min(block)}
queue = deque(seen)
while queue:
p = queue.popleft()
for step in DIRS:
q = add(p,step)
if q in block and q not in seen:
seen.add(q)
queue.append(q)
return seen == block
def project(p, axis):
return p[:axis] + p[axis+1:]
AXES = (2,0,1) # first wall J, end wall O, ceiling Y
FULL, LIGHT = cubes('full-cubes.txt'), cubes('light-cubes.txt')
assert len(FULL) == 240 and len(LIGHT) == 96 and LIGHT < FULL
assert connected(FULL) and connected(LIGHT)
VIEWS = [{project(p,i) for p in FULL} for i in AXES]
assert VIEWS == [{project(p,i) for p in LIGHT} for i in AXES]
assert [len(v) for v in VIEWS] == [64,64,52]
assert FULL == {p for p in GRID if all(project(p,i) in v for i,v in zip(AXES,VIEWS))}
assert (sum(len(v)-1 for v in VIEWS)+1)//2+1 == 90
def write_svg(file, w, h, title, desc, shapes):
(HERE/file).write_text(
f'\n', encoding='utf-8')
FACES = [
((0,-1,0), '#b4c2a3', [(0,0,0),(1,0,0),(1,0,1),(0,0,1)]),
((1,0,0), '#437266', [(1,0,0),(1,1,0),(1,1,1),(1,0,1)]),
((0,0,1), '#729481', [(0,0,1),(1,0,1),(1,1,1),(0,1,1)]),
]
def isometric(p):
x,y,z = p
return 140+10*(x-z), 24+5*(x+z)+10*y
def carving(file, block, name, desc):
shapes = ['']
for p in sorted(block, key=lambda p:(p[0]-p[1]+p[2],p)):
for direction, color, vertices in FACES:
if add(p,direction) not in block:
points = ' '.join(f'{a},{b}' for a,b in (isometric(add(p,v)) for v in vertices))
shapes.append(f'')
write_svg(file,280,244,name,desc,shapes)
carving('heavy.svg',FULL,'The 240-cube carving',
'At an angle: broad rails and upright faces frame a large hole through the green block.')
carving('light.svg',LIGHT,'The 96-cube carving',
'At the same angle and scale: an open framework of thin, stepped green rails, with gaps where the heavier carving has solid faces.')
def shadow(file, letter, view, wall):
shapes = ['']
for x,y in sorted(view):
shapes.append(f'')
write_svg(file,176,176,f'{letter} on {wall}',f'Both carvings cast this {letter}.',shapes)
for letter, view, wall in zip('JOY',VIEWS,('the first wall','the end wall','the ceiling')):
shadow(f'{letter.lower()}.svg',letter,view,wall)
print('240 and 96 cubes; both connected; matching J (64), O (64), Y (52).')