Enigmatic Code

Programming Enigma Puzzles

Enigma 1488: Cube turn

From New Scientist #2650, 5th April 2008

I have three cube-shaped dice and on each face of each die there is a non-zero digit. For each of the three dice the sum of its six different digits is the same odd total. (Incidentally, the digits are unambiguous: 6s cannot be read as 9s etc.)

I have been experimenting with these dice and, appropriately, with some numbers which are cubes. I find that it is possible to arrange the dice so that the three uppermost faces together read 125; then when I turn the three dice completely over, the new uppermost faces can be arranged to read 216. Similarly, the dice can be arranged to read 343 and then turned over and arranged to read 729.

Having had some success with cubes, I then looked at some squares. The dice can be arranged to read 169 and then turned over and arranged to read 256. And they can be arranged to read 361 and then turned over and arranged into another perfect square.

Which one?

[enigma1488]

Advertisements

One response to “Enigma 1488: Cube turn

  1. Jim Randell 20 November 2012 at 10:32 pm

    The following Python program runs in 193ms.

    from itertools import product, permutations
    from collections import defaultdict
    from enigma import irange, nconcat, is_square, printf
    
    # the three dice A[0]/A[1], A[2]/A[3], A[4],A[5] are opposite faces
    A = [0] * 6
    B = [0] * 6
    C = [0] * 6
    
    # allowable digits
    ds = set(irange(1, 9))
    
    # accumulate solutions by the missing square
    r = defaultdict(int)
    
    # the dice can be arranged to show 125
    (A[0], B[0], C[0]) = (1, 2, 5)
    # and the opposite faces can be arranged to show 216
    for (A[1], B[1], C[1]) in permutations((1, 2, 6)):
      if A[1] == A[0] or B[1] == B[0]: continue
      # the dice can be arranged to read 343
      # digits not seen so far, so let's place them on face 2
      for (A[2], B[2], C[2]) in ((3, 3, 4), (3, 4, 3), (4, 3, 3)):
        # and the opposite faces can be arranged to show 729
        for (A[3], B[3], C[3]) in permutations((2, 7, 9)):
          if A[3] == A[1] or B[3] == B[0] or C[3] == C[1]: continue
          # now fill out the remaining pair of sides on each dice
          for (A[4], A[5]) in permutations(ds.difference(A[0:4]), 2):
            # what is the sum of the faces of cube A
            s = sum(A)
            # the sum must be odd
            if s % 2 == 0: continue
            # and fill out the remaining faces of cube B
            for B[4] in ds.difference(B[0:4]):
              # the remaining face must make the same sum
              x = s - sum(B[0:5])
              if not(x in ds and x not in B[0:5]): continue
              B[5] = x
              # and fill out the remaining faces of cube C
              for C[4] in ds.difference(C[0:4]):
                # the remaining face must make the same sum
                x = s - sum(C[0:5])
                if not(x in ds and x not in C[0:5]): continue
                C[5] = x
    
                # the cubes can be arranged to read 169
                for (a, b, c) in product(irange(0, 5), repeat=3):
                  if sorted((A[a], B[b], C[c])) != [1, 6, 9]: continue
                  # and the opposite faces to read 256
                  (x, y, z) = (A[a ^ 1], B[b ^ 1], C[c ^ 1])
                  if sorted((x, y, z)) != [2, 5, 6]: continue
    
                  # and they can also be arranged to read 361
                  for (a, b, c) in product(irange(0, 5), repeat=3):
                    if sorted((A[a], B[b], C[c])) != [1, 3, 6]: continue
                    # and the other faces must make a square
                    (x, y, z) = A[a ^ 1], B[b ^ 1], C[c ^ 1]
                    for p in permutations((x, y, z)):
                      q = nconcat(p)
                      if not is_square(q): continue
                      r[q] += 1
                      printf("q={q} A={A} B={B} C={C} s={s}")
    
    # print the results
    for (k, v) in r.items():
      printf("square = {k} [{v} solutions]")
    

    Solution: The other perfect square is 529.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: