How many different cubes can you paint with six colors?
Counting
Medium
A permutations and combinations puzzle about painting a cube and turning it.
Try it first, then open the solution.
NoteCode: count the cubes
from itertools import permutations
POSITIONS = ['top', 'bottom', 'front', 'right', 'back', 'left']
SPIN = {'front': 'right', 'right': 'back', 'back': 'left', 'left': 'front'}
TIP = {'top': 'back', 'front': 'top', 'bottom': 'front', 'back': 'bottom'}
def turn(painting, move):
return tuple(painting[POSITIONS.index(move.get(p, p))] for p in POSITIONS)
def all_turns(painting):
seen, todo = {painting}, [painting]
while todo:
current = todo.pop()
for move in (SPIN, TIP):
turned = turn(current, move)
if turned not in seen:
seen.add(turned)
todo.append(turned)
return seen
paintings = set(permutations(['red', 'blue', 'green', 'yellow', 'white', 'black']))
cubes = {min(all_turns(painting)) for painting in paintings}
print(len(paintings), len(all_turns(min(paintings))), len(cubes))720 24 30