## Cube Puzzle

I got this puzzle for xmas a couple of years ago. As is normal for me, I tried half-heartedly to solve it by hand while immediately scheming a way to solve it programmatically.

I had totally forgotten about it because it got buried in our game closet but I found it a couple of weeks ago while looking for something else. I realized I had never solved it so I set about writing a python script to do it.

I realized right away that the order the cubes didn’t matter, just the final orientation of each cube. So my basic strategy was to create a candidate set of orientations for each cube, check to see if it’s solved and if not rotate each cube through all orientations until a solution is found.

I took each cube and put a small post-it note on each one so I could temporarily tell them apart and entered the cube color for each side of each cube. Next instead of ’re-orienting’ each cube, I decided to create a set of index permutations so I could get the color value for each cube for a particular orientation. Then it was just looping each cube (4) through each possible orientation (24) for a total search space of 331,776.

After some debugging I ended up with a sucessful run.

``````8 solutions
total count of tests 331776
``````

Hmmm, if there was just a singular solution I still would have expected 4 solutions because my program did not account for the rotation of each solution. (e.g. each side of solution takes a turn being on top. like rolling a piece of square stock.)

But since there are 8 solutions found by my program, that tells me there are 2 distinct solutions with some cubes oriented differently with relationship to each other.

Next I might try to solve this by formulating it as a Constraint Satisfaction Problem and using MiniZinc to try to solve it.

I have played around with this language before but have only done example problems, I’ve never had anything ‘real’ to try to solve. (although I would not really count solving a puzzle that I have already solved by other means a ‘real’ problem.) I imagine the biggest issue would be getting it modeled correctly so the solver would have a good search space to iterate through.

In the past I wrote an application for an event at my kid’s school that definitely could have used the features of a constraint satisfaction solver but I never revisited that system because my kids left that school after the 5th grade. It is definitely an area of programming I am interested in and hopefully I will find a problem that would be best solved by CSP with the right amount of time to come up with a solution so I can really learn MiniZinc (or z3 or prolog or GECODE or ???)