Enigmatic Code

Programming Enigma Puzzles

Enigma 1417: Magic circle

From New Scientist #2577, 11th November 2006

Joe placed 10 numbered counters in a circle (as shown) and asked Penny to pick a number. Then he asked her to count clockwise that number of counters to find a new counter. For example, if she picked 7 the new counter would be 1. Repeating the process with the number shown on each new counter she would finish up at zero.

Enigma 1417

But if she picked one particular number first she would reach zero via all the other counters. Penny found that leaving some of the counters in place, including 0, 1 and 2, there was another arrangement of the counters that had this same property if she started at the same number.

Beginning with zero, what was the clockwise order of Penny’s numbers?

[enigma1417]

Advertisements

One response to “Enigma 1417: Magic circle

  1. Jim Randell 29 June 2013 at 6:32 pm

    This puzzle is similar to Enigma 1421.

    This Python 3 program (it uses the yield from construct) runs in 173ms. It would probably be more efficient to check the potential sequences forward starting from the start number we’ve determined, but we’ve already written the solve() routine, so we use that and check if any solution starts from the number we want.

    from itertools import permutations
    from enigma import printf
    
    # the sequence from the diagram
    D = (0, 2, 7, 5, 3, 9, 6, 4, 8, 1)
    
    def solve(s, p, r):
      n = len(s)
      # are all counters used?
      if len(r) == n:
        yield r
      else:
        # find counters that point to this one
        for i in range(1, n):
          if s[(p - i) % n] == i and i not in r:
            yield from solve(s, p - i, [i] + r)
    
    # find solutions for the diagram, and report start number
    s = set()
    for r in solve(D, 0, [0]):
      s.add(r[0])
      printf("{D} => {r}")
    # there should be only one solution
    assert len(s) == 1
    s = s.pop()
    printf("[start={s}]")
    
    # now rearrange the remaining numbers (0, 1, 2 stay put)
    for p in permutations((3, 4, 5, 6, 7, 8, 9)):
      d = (0, 2) + p + (1,)
      if d == D: continue
      for r in solve(d, 0, [0]):
        if r[0] == s:
          printf("{d} => {r}")
    

    Solution: Penny’s numbers are: 0, 2, 6, 4, 7, 9, 3, 5, 8, 1.

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: