Enigmatic Code

Programming Enigma Puzzles

Enigma 406: The ritual

From New Scientist #1556, 16th April 1987 [link]

I entered the jungle clearing and found, at the centre, six stones numbered 1, 2, 3, 4, 5, 6. My native guide demonstrated the ancient ritual which was enacted on that site.

First I had to arrange the stones in any order I wished, so I put them as 421365. I then noted the number on the first stone — all counting is from the left — is was 4, and so I picked up the fourth stone and placed it at the right hand end. I obtained 421653. Next, I looked at the second stone, a 2, and moved the second stone to the end, to give 416532. I repeated the procedure for the third stone to give 416532, and then for the fourth, fifth and sixth stones, to give successively, 416523. 465231, 652314. The final arrangement was called the result of the ritual.

Just then the high priestess, Sarannah, entered. She arranged the stones in a certain order, carried out the ritual and obtained the result 314625. My guide explained that the arrangement Sarannah started with was the result of applying the ritual to a very special arrangement, which she could not tell me.

What was the arrangement that Sarannah started with?



One response to “Enigma 406: The ritual

  1. Jim Randell 21 July 2017 at 8:30 am

    This code considers the result of applying the ritual to every possible arrangement of stones, and builds a “reverse map” which tells us what arrangements will lead to a particular outcome. We can then use the reverse map to solve the problem. It runs in 35ms.

    from itertools import permutations
    from collections import defaultdict
    from enigma import irange, printf
    # perform the ritual on tuple <t>
    def ritual(t):
      for i in irange(0, 5):
        # examine the i'th digit
        n = t[i] - 1
        # move the nth digit to the end
        t = t[:n] + t[n + 1:] + (t[n],)
      return t
    # check the example given in the puzzle
    assert ritual((4, 2, 1, 3, 6, 5)) == (6, 5, 2, 3, 1, 4)
    # make a reverse map of the ritual
    r = defaultdict(list)
    for t in permutations((1, 2, 3, 4, 5, 6)):
    # consider: t0 => t1 => 314625
    t2 = (3, 1, 4, 6, 2, 5)
    for t1 in r[t2]:
      for t0 in r[t1]:
        printf("{t0} => {t1} => {t2}")

    Solution: Sarannah started with 623514.

    The “special arrangement” must be one of 152436, 154326, 524316, 543216 or 546321, but we can’t tell which one.

    But without this fact Sarannah could have started with 263451 or 632514, but neither of these can be the result of applying the ritual to any arrangement of the stones, so they can be eliminated.

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: