Enigmatic Code

Programming Enigma Puzzles

Enigma 1462: Coloured cubes

From New Scientist #2623, 29th September 2007

In the following statements different capital letters in bold stand for different digits, with the same letter consistently standing for the same digit.

I have a collection of cubes in three different colours: red, blue and yellow. Their sides are all whole numbers, and furthermore I can tell you that the volume of each red cube is NIL, the face of each blue cube has NO area and the volume of each yellow cube is ZERO. Obviously the total volume of all the cubes is NOTHING. As for quantities, if you arrange the cubes into three distinct piles according to colour, you will find that in one pile there are NO cubes and in another pile there are NONE.

How many cubes are in the remaining pile, and what is their colour?


One response to “Enigma 1462: Coloured cubes

  1. Jim Randell 12 February 2013 at 9:24 am

    This Python program runs in 40ms.

    from itertools import permutations
    from enigma import irange, is_duplicate, concat, printf
    # make a dictionary mapping values to the result of a function
    # applied to that value, as a string, where the string has no repeats
    def data(f, i):
      d = dict()
      for x in i:
        s = str(f(x))
        if not is_duplicate(s): d[x] = s
      return d
    # two digit squares
    s3 = data(lambda x: x ** 2, irange(4, 9))
    # three and four digit cubes
    c3 = data(lambda x: x ** 3, irange(5, 9))
    c4 = data(lambda x: x ** 3, irange(10, 21))
    # blue cubes have faces with area NO
    for (b, NO) in s3.items():
      # red cubes have volume NIL
      for (r, NIL) in (c for c in c3.items() if c[1][0] == NO[0]):
        # yellow cubes have volume ZERO
        for (y, ZERO) in (c for c in c4.items() if c[1][-1] == NO[1]):
          # remaining digits
          ds = set('0123456789').difference(NIL, ZERO)
          # there should be 3 remaining digits
          if len(ds) != 3: continue
          # and these are (in some order) T, H, G
          for (T, H, G) in permutations(ds):
            NOTHING = int(NO + T + H + NIL[1::-1] + G)
            NONE = int(NO + NO[0] + ZERO[1])
            # consider the three piles (the colours in some order)
            colour = { b: 'blue', r: 'red', y: 'yellow' }
            for (p1, p2, p3) in permutations((b, r, y)):
              v1 = int(NO) * p1 ** 3
              v2 = int(NONE) * p2 ** 3
              v3 = int(NOTHING) - v1 - v2
              (n, x) = divmod(v3, p3 ** 3)
              if x > 0: continue
              printf("b={b} r={r} y={y} NO={NO} NIL={NIL} ZERO={ZERO} NONE={NONE} NOTHING={NOTHING}")
              printf("pile 1: (NO) {NO} {c} cubes of side {p1}, volume {v1}", c=colour[p1])
              printf("pile 2: (NONE) {NONE} {c} cubes of side {p2}, volume {v2}", c=colour[p2])
              printf("pile 3: {n} {c} cubes of side {p3}, volume {v3}", c=colour[p3])

    Solution: There are 21,413 blue cubes in the third pile.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: