# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1421: Vicious circle

From New Scientist #2581, 9th December 2006

Joe’s latest puzzle for Penny consists of nine consecutively numbered pegs and a disc with 10 holes, which is free to rotate on a small board. The problem is to insert all nine pegs in the disc.

A peg may only be inserted in the disc in an empty hole adjacent to the arrow. The disc is then rotated clockwise the number of holes equal to the number on that peg. So in the picture, if peg 6 is inserted, the disc is then rotated until the peg is in the position where peg 3 is shown.

The final position of the pegs in Penny’s solution are shown above, but she cannot remember the positions of the pegs numbered 4, 5 and 6.

In what order did Penny insert the nine pegs?

[enigma1421]

### One response to “Enigma 1421: Vicious circle”

1. Jim Randell 15 June 2013 at 11:28 am

This Python program runs in 36ms.

```from itertools import permutations
from enigma import irange, printf

def solve(D, p, r):
# are all 9 pegs placed?
if len(r) == 9:
printf("pegs={r} [disc={D}]")
return
# rotate the disc until a possible peg is pointed to
for i in irange(1, 9):
if D[(p + i) % 10] == i and i not in r:
solve(D, p + i, [i] + r)

# final arrangement of the disc
#     0  1  2  3  4  5  6  7  8  9
D = [ 0, 9, 0, 2, 8, 0, 3, 7, 0, 1 ]

# the pegs in position 2, 5 and 8 are numbered 4, 5 and 6 (in some order)
for (D[2], D[5], D[8]) in permutations((4, 5, 6)):
solve(D, 0, [])
```

Solution: The pegs were inserted in the following order: 6, 1, 4, 8, 3, 2, 9, 5, 7.