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:

my $cube1 = 'pgpygr'; my $cube2 = 'rprrgy'; my $cube3 = 'ppryyg'; my $cube4 = 'rrygpy';
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:
+-------+ | g | | green | | | +-------+-------+-------+-------+ | p | g | p | y | |purple | green |purple |yellow | | | | | | +-------+-------+-------+-------+ | r | | red | | | +-------+

For those not interested in trying to solve it themselves, my program is behind the following spoiler tags.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Challenge: "Insanity" Cube Puzzle by liverpole

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.