|Perl: the Markov chain saw|
Challenge: "Insanity" Cube Puzzleby liverpole (Monsignor)
|on Jul 04, 2006 at 22:47 UTC||Need Help??|
Last weekend during our annual family reunion it rained the whole time, so we played a lot of indoor games.
One game which my niece was trying to solve was called Insanity. It consisted simply of a set of 4 cubes with differently colored faces. The colors were green, purple, red and yellow, and the object was to stack the cubes on top of one another in such a way as to have no duplicate colors in any of the 4 columns.
I thought it would be fun to write a Perl script to solve it using "brute force", but didn't get it finished until after we returned Sunday night. I was mostly interested in seeing whether there was only a single solution, or multiple solutions -- in addition, of course, to the enjoyment of writing a fun Perl script. I'm presenting this as a challenge in case other monks would like to try it for themselves. I'm fairly sure someone can come up with a more elegant solution than my "brute force" method, or at least improve on its readability and/or speed.
The following represents the 4 cubes:
where 'g', 'p', 'r' and 'y' represent the colors 'green', 'purple', 'red' and 'yellow' respectively, and the colors in each string are, in order, the left, front, right, back, top and bottom faces. Thus, cube1 'pgpygr' represents the cube:
For those not interested in trying to solve it themselves, my program is behind the following spoiler tags.