For Christmas, I got the ThinkFun 36 Cube. It is consists of 36 towers in 6 colors and 6 different sizes and a base plate with 6 by 6 slots to plug in the towers. These slots are of different heights. The goal is to place one towers of every color in each row and column. And the towers must fit to form a level cube.
After some tries, I came to the conclusion that this puzzle is the work of the devil and that I should not waste more brain cycles on solving it. So I wrote a little python script to solve the puzzle for me.
Show sourcecode
#!/usr/bin/python
COLORS = dict(zip((1 << i for i in range(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0 for i in range(6)]
cube = ((1,3,4,5,2,0),
(2,5,0,4,1,3),
(0,1,3,2,5,4),
(5,4,1,3,0,2),
(4,2,5,0,3,1),
(3,0,2,1,4,5))
solution = [[False for x in range(6)] for x in range(6)]
used = tuple(set() for i in range(6))
def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been used
if color & used_towers[size]:
return False
#check if tower has already been used in row or column:
for i in range(6):
if color == solution[pos/6][i]: return False
if color == solution[i][pos%6]: return False
return True
def print_solution(solution):
for i in range(6):
print zip((COLORS[c] for c in solution[i]), (6 - s for s in cube[i]))
def solve(pos, solution, used_towers, best):
if pos == 36: return best, solution
size = cube[pos/6][pos%6]
for i in range(6):
color = 1 << i
if test_color(pos, solution, used_towers, color, size):
used_towers[size] = used_towers[size] | color
solution[pos/6][pos%6] = color
if pos > best:
print "#######", pos + 1, "towers placed"
print_solution(solution)
best = pos
best, solution = solve(pos+1, solution, used_towers, best)
if best >= 35:
return best, solution
solution[pos/6][pos%6] = False
used_towers[size] = used_towers[size] ^ color
return best, solution
solve(0, solution, used_towers, 0) |
#!/usr/bin/python
COLORS = dict(zip((1 << i for i in range(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0 for i in range(6)]
cube = ((1,3,4,5,2,0),
(2,5,0,4,1,3),
(0,1,3,2,5,4),
(5,4,1,3,0,2),
(4,2,5,0,3,1),
(3,0,2,1,4,5))
solution = [[False for x in range(6)] for x in range(6)]
used = tuple(set() for i in range(6))
def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been used
if color & used_towers[size]:
return False
#check if tower has already been used in row or column:
for i in range(6):
if color == solution[pos/6][i]: return False
if color == solution[i][pos%6]: return False
return True
def print_solution(solution):
for i in range(6):
print zip((COLORS[c] for c in solution[i]), (6 - s for s in cube[i]))
def solve(pos, solution, used_towers, best):
if pos == 36: return best, solution
size = cube[pos/6][pos%6]
for i in range(6):
color = 1 << i
if test_color(pos, solution, used_towers, color, size):
used_towers[size] = used_towers[size] | color
solution[pos/6][pos%6] = color
if pos > best:
print "#######", pos + 1, "towers placed"
print_solution(solution)
best = pos
best, solution = solve(pos+1, solution, used_towers, best)
if best >= 35:
return best, solution
solution[pos/6][pos%6] = False
used_towers[size] = used_towers[size] ^ color
return best, solution
solve(0, solution, used_towers, 0)
My program quickly came up with a correct placement for 34 towers – but it failed to find the complete solution.
[('P', 5), ('Y', 3), ('O', 2), ('B', 1), ('R', 4), ('G', 6)]
[('Y', 4), ('O', 1), ('P', 6), ('R', 2), ('G', 5), ('B', 3)]
[('O', 6), ('B', 5), ('R', 3), ('G', 4), ('P', 1), ('Y', 2)]
[('R', 1), ('G', 2), ('Y', 5), ('P', 3), ('B', 6), ('O', 4)]
[('B', 2), ('P', 4), ('G', 1), ('Y', 6), ('O', 3), ('R', 5)]
[('G', 3), ('R', 6), ('B', 4), ('O', 5), ('X', 2), ('X', 1)]
Legend:
P = Purple, Y = Yellow, O = Orange, B = Blue, R = Red, G = Green, X = Empty
The number is the size of the tower.
As you can see, I didn’t waste much time on making the output pretty 🙂
So close and yet so far
After spending lots of time verifying that my program was working correctly, I became impatient and googled for help. I found an answer, but it revealed too much, taking all the fun.
Therefore, I split my solution into multiple hints. If you are stuck, reveal just one of them at a time and try to figure it out by yourself. It is way more rewarding!
Hint #1 (show): Your assumptions are probably wrong.
Now go back and try to solve it. I’m waiting here.
Hint #2 (show): The assumption that all towers of the same size only differ in color is wrong.
Hint #3 (show): There are two towers which do fit on slots where the other towers of the same size do not fit.
Hint #4 (show): The two special towers are the orange one of heigh 5 and the red one of height 6.
Hint #5 (show): The orange tower of height 5 has to go to position (1,2) and the red tower of height 6 to (3,2) in my coordinate system.
Even if you uncovered all hints, the puzzle is still far from solved. You can still tinker with it forever.
Spoiler alert: Don’t uncover the solution, unless you are really desperate!
Click to show the 36cube solution.
After I had found the two culprits, I adjusted my program by changing the array and inserting two special conditions. Within seconds I had a complete solution of the 36 Cube:
[('P', 5), ('Y', 3), ('B', 2), ('O', 1), ('R', 4), ('G', 6)]
[('B', 4), ('P', 1), ('Y', 5), ('G', 2), ('O', 5), ('R', 3)]
[('R', 6), ('G', 5), ('P', 3), ('Y', 4), ('B', 1), ('O', 2)]
[('G', 1), ('R', 2), ('O', 6), ('B', 3), ('Y', 6), ('P', 4)]
[('Y', 2), ('O', 4), ('R', 1), ('P', 6), ('G', 3), ('B', 5)]
[('O', 3), ('B', 6), ('G', 4), ('R', 5), ('P', 2), ('Y', 1)]
Finally solved
#!/usr/bin/python
COLORS = dict(zip((1 << i for i in range(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0 for i in range(6)]
# definition with (1,2) and (3,2) switched
cube = ((1,3,4,5,2,0),
(2,5,1,4,1,3),
(0,1,3,2,5,4),
(5,4,0,3,0,2),
(4,2,5,0,3,1),
(3,0,2,1,4,5))
solution = [[False for x in range(6)] for x in range(6)]
used = tuple(set() for i in range(6))
def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been used
if color & used_towers[size]:
return False
#check if tower has already been used in row or column:
for i in range(6):
if color == solution[pos/6][i]: return False
if color == solution[i][pos%6]: return False
# special conditions for the two special towers
if (pos/6, pos%6) == (1,2) and color != 2: return False
if (pos/6, pos%6) == (3,2) and color != 4: return False
return True
def print_solution(solution):
for i in range(6):
print zip((COLORS[c] for c in solution[i]), (6 - s for s in cube[i]))
def solve(pos, solution, used_towers, best):
if pos == 36: return best, solution
size = cube[pos/6][pos%6]
for i in range(6):
color = 1 << i
if test_color(pos, solution, used_towers, color, size):
used_towers[size] = used_towers[size] | color
solution[pos/6][pos%6] = color
if pos > best:
print "#######", pos + 1, "towers placed"
print_solution(solution)
best = pos
best, solution = solve(pos+1, solution, used_towers, best)
if best >= 35:
return best, solution
solution[pos/6][pos%6] = False
used_towers[size] = used_towers[size] ^ color
return best, solution
solve(0, solution, used_towers, 0) |
#!/usr/bin/python
COLORS = dict(zip((1 << i for i in range(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0 for i in range(6)]
# definition with (1,2) and (3,2) switched
cube = ((1,3,4,5,2,0),
(2,5,1,4,1,3),
(0,1,3,2,5,4),
(5,4,0,3,0,2),
(4,2,5,0,3,1),
(3,0,2,1,4,5))
solution = [[False for x in range(6)] for x in range(6)]
used = tuple(set() for i in range(6))
def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been used
if color & used_towers[size]:
return False
#check if tower has already been used in row or column:
for i in range(6):
if color == solution[pos/6][i]: return False
if color == solution[i][pos%6]: return False
# special conditions for the two special towers
if (pos/6, pos%6) == (1,2) and color != 2: return False
if (pos/6, pos%6) == (3,2) and color != 4: return False
return True
def print_solution(solution):
for i in range(6):
print zip((COLORS[c] for c in solution[i]), (6 - s for s in cube[i]))
def solve(pos, solution, used_towers, best):
if pos == 36: return best, solution
size = cube[pos/6][pos%6]
for i in range(6):
color = 1 << i
if test_color(pos, solution, used_towers, color, size):
used_towers[size] = used_towers[size] | color
solution[pos/6][pos%6] = color
if pos > best:
print "#######", pos + 1, "towers placed"
print_solution(solution)
best = pos
best, solution = solve(pos+1, solution, used_towers, best)
if best >= 35:
return best, solution
solution[pos/6][pos%6] = False
used_towers[size] = used_towers[size] ^ color
return best, solution
solve(0, solution, used_towers, 0)