DriveYaNuts.frink

View or download DriveYaNuts.frink in plain text format


/** Solver for the Drive Ya Nuts puzzle:

   http://www.samstoybox.com/toys/DriveYaNuts.html

   solution:

   http://www.hasbro.com/common/instruct/DriveYaNuts.PDF   
*/


nuts = [[1,2,3,4,5,6],
        [1,4,3,6,5,2],
        [1,4,6,2,3,5],
        [1,6,2,4,5,3],
        [1,6,4,2,5,3],
        [1,6,5,3,2,4],
        [1,6,5,4,3,2]]

/** Now make a new array of dictionaries which map from
    (number on nut) to (position on nut) where (position on nut) is 0-based. */

nutPositions = new array
for nut=nuts
{
   nutPos = new dict
   
   for i=0 to 5
      nutPos@(nut@i) = i

   nutPositions.push[nutPos]
}

nutNums = array[0 to 6]
for pos = nutNums.permute[]
{
   nut0index = pos@0

   nut0pos0 = nuts@nut0index@0
   nut1index = pos@1
   nut1rotation = (3 - nutPositions@nut1index@nut0pos0) mod 6

   nut2index = pos@2
   nut0pos1 = nuts@nut0index@1
   nut2rotation = (4 - nutPositions@nut2index@nut0pos1) mod 6
   nut1pos2 = nuts@nut1index@((2 - nut1rotation) mod 6)
   nut2pos5 = nuts@nut2index@((5 - nut2rotation) mod 6)
   if (nut1pos2 != nut2pos5)
      next

   nut3index = pos@3
   nut0pos2 = nuts@nut0index@2
   nut3rotation = (5 - nutPositions@nut3index@nut0pos2) mod 6
   nut2pos3 = nuts@nut2index@((3 - nut2rotation) mod 6)
   nut3pos0 = nuts@nut3index@((0 - nut3rotation) mod 6)
   if (nut2pos3 != nut3pos0)
      next
   
   nut4index = pos@4
   nut0pos3 = nuts@nut0index@3
   nut4rotation = (0 - nutPositions@nut4index@nut0pos3) mod 6
   nut3pos4 = nuts@nut3index@((4 - nut3rotation) mod 6)
   nut4pos1 = nuts@nut4index@((1 - nut4rotation) mod 6)
   if (nut3pos4 != nut4pos1)
      next
   
   nut5index = pos@5
   nut0pos4 = nuts@nut0index@4
   nut5rotation = (1 - nutPositions@nut5index@nut0pos4) mod 6
   nut4pos5 = nuts@nut4index@((5 - nut4rotation) mod 6)
   nut5pos2 = nuts@nut5index@((2 - nut5rotation) mod 6)
   if (nut4pos5 != nut5pos2)
      next
   
   nut6index = pos@6
   nut0pos5 = nuts@nut0index@5
   nut6rotation = (2 - nutPositions@nut6index@nut0pos5) mod 6
   nut5pos0 = nuts@nut5index@((0 - nut5rotation) mod 6)
   nut6pos3 = nuts@nut6index@((3 - nut6rotation) mod 6)
   if (nut5pos0 != nut6pos3)
      next
   
   nut6index = pos@6
   nut6pos1 = nuts@nut6index@((1 - nut6rotation) mod 6)
   nut1pos4 = nuts@nut1index@((4 - nut1rotation) mod 6)
   if (nut6pos1 != nut1pos4)
      next
   
   println["Matching:  $pos"]

   println["Nut $nut0index " + nuts@nut0index + " is at the center,"]
   println[" with digit $nut0pos0 at its top."]

   println["Clockwise from top are nuts:"]
   for i=1 to 6
      println["   " + pos@i + " " + nuts@(pos@i)]
}


View or download DriveYaNuts.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17591 days, 17 hours, 42 minutes ago.