Yesterday I gave a presentation at PyCologne about Python packages. It was meant as a kind of sequel to the “Cooking Eggs” talk given by Christopher Arndt (in German) in September. While he presented ways to create Python packages, I focused on how to use Python packages.
Python comes with batteries included, but sometimes, battery power is not enough. Thankfully, there are countless packages available for use. Given that there “should be one– and preferably only one –obvious way to do it,” using Python packages can be quite intimidating. The documentation of your package might say to download, unzip und run python setup.py install. Meet distutils. Or just do easy_install MyPackage, which is uses setuptools. But then you hear that setuptools is superseeded by distribute and instead of easy_install, you should use pip. And then there is virtualenv, which is awesome, but can be even more awesome with virtualenvwrapper. What a mess!
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.
#!/usr/bin/python
COLORS = dict(zip((1<< i for i inrange(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0for i inrange(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 = [[Falsefor x inrange(6)]for x inrange(6)]
used = tuple(set()for i inrange(6))def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been usedif color & used_towers[size]:
returnFalse#check if tower has already been used in row or column:for i inrange(6):
if color == solution[pos/6][i]: returnFalseif color == solution[i][pos%6]: returnFalsereturnTruedef print_solution(solution):
for i inrange(6):
printzip((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 inrange(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.
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 to 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 yellow one of heigh 5 and the orange one of height 6.
Hint #5 (show): The yellow tower of height 5 has to go to position (1,2) and the orange 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 your are really desperate!
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:
#!/usr/bin/python
COLORS = dict(zip((1<< i for i inrange(6)), ('P','Y','O','B','R','G')))
COLORS[False] = 'X'
used_towers = [0for i inrange(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 = [[Falsefor x inrange(6)]for x inrange(6)]
used = tuple(set()for i inrange(6))def test_color(pos, solution, used_towers, color, size):
#check if tower of this size and color has already been usedif color & used_towers[size]:
returnFalse#check if tower has already been used in row or column:for i inrange(6):
if color == solution[pos/6][i]: returnFalseif color == solution[i][pos%6]: returnFalse# special conditions for the two special towersif(pos/6, pos%6) == (1,2)and color != 2: returnFalseif(pos/6, pos%6) == (3,2)and color != 4: returnFalsereturnTruedef print_solution(solution):
for i inrange(6):
printzip((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 inrange(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)