Enigmatic Code

Programming Enigma Puzzles

Enigma 1460: Colour coded

From New Scientist #2621, 15th September 2007

I have nine cards, each of a different colour. On each of the cards is a different non-zero digit. I have spaced the cards evenly round the circumference of a circle. All except the grey card are between an odd digit and an even digit. The hazel, indigo, jade and khaki cards are consecutive in that order clockwise around the circle, with the digits on the first three adding to give the digit on the fourth. The lemon, mauve and navy cards are consecutive in that order clockwise around the circle, with the digits of the first two adding to give double the digit on the third. There is also an orange card.

In some cases the digit on a card equals the sum of its neighbouring two digits. In how many cases? The answer is the digit on the hazel card. Starting at 1, list the numbers and their colours clockwise around the circle (e.g. 1J, 5L …).



One response to “Enigma 1460: Colour coded

  1. Jim Randell 22 February 2013 at 9:32 am

    This Python program runs in 55ms.

    from itertools import permutations
    from enigma import irange, printf
    # "on each of the [nine] cards is a different non-zero digit"
    # so the nine digits are: 1 2 3 4 5 6 7 8 9
    # which gives five odd digits: 1 3 5 7 9
    # and four even digits: 2 4 6 8
    # "all except the grey card are between an odd digit and an even digit"
    # so if we put grey in position 0, the possible sequences are:
    # O O E E O O E E O = five odd digits and four even digits
    # E E O O E E O O E = five even digits and four odd digits
    # so the first of these is the one we want
    # considering odds and evens separately
    for (d1, d2, d5, d6, d9) in permutations((1, 3, 5, 7, 9)):
      for (d3, d4, d7, d8) in permutations((2, 4, 6, 8)):
        # make the digit sequence
        d = (d1, d2, d3, d4, d5, d6, d7, d8, d9)
        # possible positions for h, i, j, k
        for k in irange(4, 8):
          if d[k] != sum(d[k - 3:k]): continue
          # and for l, m, n
          for n in irange(3, 8):
            if 2 * d[n] != sum(d[n - 2:n]): continue
            # and the one that's left is o
            # fill out the colours
            c = ['G'] + ['O'] * 8
            c[k - 3:k + 1] = 'HIJK'
            c[n - 2:n + 1] = 'LMN'
            if c.count('O') != 1: continue
            # how many digits are the sum of their immediate neighbours?
            h = sum(1 for i in irange(0, 8) if d[i] == d[(i - 1) % 9] + d[(i + 1) % 9])
            # it should be d[h] = d[k - 3]
            if h != d[k - 3]: continue
            # make pairs of digit + colour
            ps = list(str(d[i]) + c[i] for i in irange(0, 8))
            # start from d=1
            while ps[0][0] != '1': ps.append(ps.pop(0))
            # output the soltion
            print(' '.join(ps))

    Solution: The cards are 1G, 3H, 2I, 4J, 9K, 5O, 8L, 6M, 7N.

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

%d bloggers like this: