Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
22-liner: fast & clean solution in Clear category for Broken Window by przemyslaw.daniel
def sliced(piece):
return sum(([x, y] for x, y in zip(piece, piece[1:])), [])
def is_incorrect(up, down):
return len({x+y for x, y in zip(up, down)}) > 1
def broken_window(pieces):
stack = [([], [], [], [], list(enumerate(pieces)))]
while stack:
up, down, up_idx, down_idx, pieces = stack.pop()
if is_incorrect(up, down):
continue
if not pieces and len(up) == len(down):
return up_idx, down_idx
for k in range(len(pieces)):
(index, piece), _pieces = pieces[k], pieces[:k]+pieces[k+1:]
if len(up) < len(down):
piece = sliced(piece[::-1])
stack += [(up+piece, down, up_idx+[index], down_idx, _pieces)]
else:
piece = sliced(piece)
stack += [(up, down+piece, up_idx, down_idx+[index], _pieces)]
March 22, 2019
Comments: