Skip to content

Instantly share code, notes, and snippets.

@gudnm
Created June 30, 2017 00:29
Show Gist options
  • Save gudnm/e3ca8f57b3107842b8102cfbcbcbddb0 to your computer and use it in GitHub Desktop.
Save gudnm/e3ca8f57b3107842b8102cfbcbcbddb0 to your computer and use it in GitHub Desktop.
The dumbest solution for eraser cube puzzle
from itertools import permutations
pieces = [[0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1]]
def rotations(piece):
res = []
for i in range(4):
res += [piece[i*3:]+piece[:i*3]]
return res
def match(pieces):
# check 8 corners (4 bottom and 4 on top)
if (pieces[0][0] + pieces[1][9] + pieces[4][6] != 1 or
pieces[0][3] + pieces[2][9] + pieces[1][6] != 1 or
pieces[0][6] + pieces[3][9] + pieces[2][6] != 1 or
pieces[0][9] + pieces[4][9] + pieces[3][6] != 1 or
pieces[5][9] + pieces[1][0] + pieces[4][3] != 1 or
pieces[5][0] + pieces[4][0] + pieces[3][3] != 1 or
pieces[5][3] + pieces[3][0] + pieces[2][3] != 1 or
pieces[5][6] + pieces[2][0] + pieces[1][3] != 1):
return False
# check 12 edges (4 bottom, 4 on top and 4 sides), 2 cells for each
if (pieces[0][1] + pieces[1][8] != 1 or
pieces[0][2] + pieces[1][7] != 1 or
pieces[0][4] + pieces[2][8] != 1 or
pieces[0][5] + pieces[2][7] != 1 or
pieces[0][7] + pieces[3][8] != 1 or
pieces[0][8] + pieces[3][7] != 1 or
pieces[0][10] + pieces[4][8] != 1 or
pieces[0][11] + pieces[4][7] != 1 or
pieces[5][8] + pieces[1][1] != 1 or
pieces[5][7] + pieces[1][2] != 1 or
pieces[5][5] + pieces[2][1] != 1 or
pieces[5][4] + pieces[2][2] != 1 or
pieces[5][2] + pieces[3][1] != 1 or
pieces[5][1] + pieces[3][2] != 1 or
pieces[5][11] + pieces[4][1] != 1 or
pieces[5][10] + pieces[4][2] != 1 or
pieces[1][4] + pieces[2][11] != 1 or
pieces[1][5] + pieces[2][10] != 1 or
pieces[2][4] + pieces[3][11] != 1 or
pieces[2][5] + pieces[3][10] != 1 or
pieces[3][4] + pieces[4][11] != 1 or
pieces[3][5] + pieces[4][10] != 1 or
pieces[4][4] + pieces[1][11] != 1 or
pieces[4][5] + pieces[1][10] != 1):
return False
return True
def solve(pieces):
solutions = [] # we're going to find all the possible solutions
r0 = pieces[0]
for r1 in rotations(pieces[1]):
for r2 in rotations(pieces[2]):
for r3 in rotations(pieces[3]):
for r4 in rotations(pieces[4]):
for r5 in rotations(pieces[5]):
for perm in list(permutations([r1, r2, r3, r4, r5])):
if match([r0] + list(perm)):
solutions.append([r0] + list(perm))
return solutions
solve(pieces)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment