Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
DFS solution in Clear category for Stacking Cubes by r_tchaik
from typing import List, Tuple
def isvalid(p1, p2):
return all([(min(p1[i] + p1[2], p2[i] + p2[2]) - max(p1[i], p2[i])) > 0 for i in range(2)])
def stacking_cubes(cubes: List[Tuple[int, int, int]], current=None) -> int:
valid = {idx: item for idx, item in enumerate(cubes) if current is None or isvalid(current, item)}
return 0 if not valid else max(
[valid[idx][2] + stacking_cubes(cubes[:idx] + cubes[idx + 1:], valid[idx]) for idx in valid])
if __name__ == '__main__':
assert stacking_cubes([(0, 0, 2), (1, 1, 2), (3, 2, 2)]) == 4, 'basic'
assert stacking_cubes([(0, 0, 2), (1, 1, 2), (1, 2, 1), (2, 2, 2)]) == 6, 'basic 2'
assert stacking_cubes([(0, 0, 2), (2, 0, 2), (2, 0, 2), (0, 2, 2), (0, 2, 2), (0, 2, 2), (0, 2, 2)]) == 8, 'towers'
assert stacking_cubes([(0, 0, 2), (0, 3, 2), (3, 0, 2)]) == 2, 'no stacking'
assert stacking_cubes([(-1, -1, 2), (0, 0, 2), (-2, -2, 2)]) == 6, 'negative coordinates'
print("Coding complete? Click 'Check' to earn cool rewards!")
April 19, 2020