Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Listed half the permutations, reversed the rest; nested loop solution in Clear category for The Tower by kkkkk
def all_rotations(cube):
"""Return list of rotated strings for given cube."""
# Incoming string represents front, right, left, back, top, bottom.
# Or the indices - 0, 1, 2, 3, 4, 5.
# The following represents the sides for 12 positions when rotating a
# cube. Reversing each item in the list provides the remaining 12
# positions.
indices = [
[0, 1, 2, 3],
[1, 3, 0, 2],
[3, 1, 2, 0],
[2, 3, 0, 1],
[0, 4, 5, 3],
[4, 3, 0, 5],
[0, 5, 4, 3],
[5, 3, 0, 4],
[1, 4, 5, 2],
[4, 2, 1, 5],
[1, 5, 4, 2],
[5, 2, 1, 4],
]
rotations = []
for index_list in indices:
rotations.append(''.join([cube[idx] for idx in index_list]))
rotations.append(''.join([cube[idx] for idx in index_list][::-1]))
return rotations
def any_matched_sides(cube, rotations):
"""Return True if the cube matches any rotation of the other cube."""
for rotation in rotations:
if all(c1 == c2 for c1, c2 in zip(cube, rotation)):
return True
return False
def tower(cubes):
"""Return maximum height of a tower with matching colors on all sides."""
# Obtain a list of all possible rotations for all cubes.
cube_rotations = []
for cube in cubes:
cube_rotations.append(all_rotations(cube))
# For each cube, compare it against all the rotations of each of
# the other cubes.
max_height = 0
for cube in cubes:
match_cnt = 0
for rotation_list in cube_rotations:
if any_matched_sides(cube, rotation_list):
match_cnt += 1
if match_cnt > max_height:
max_height = match_cnt
# Early exit if all cubes match.
if max_height == len(cubes):
break
return max_height
Jan. 13, 2020
Comments: