### Random Post

### Recent Posts

- Enigma 1065: Cute cubes
- Enigma 444: Rows and rows
- Puzzle 50: Football and addition
- Enigma 1066: Members of the clubs
- Enigma 443: The bells they are a-changing
- Tantalizer 455: Ballistico
- Tantalizer 456: Square deal
- Enigma 1067: Bye!
- Enigma 442b: Oh yes I did! Oh no you didn’t!
- Puzzle 51: A multiplication

### Recent Comments

Brian Gladman on Enigma 1065: Cute cubes | |

Jim Randell on Enigma 1065: Cute cubes | |

geoffrounce on Enigma 444: Rows and rows | |

Jim Randell on Enigma 444: Rows and rows | |

geoffrounce on Enigma 1611: Three sister… |

### Archives

### Categories

- article (11)
- enigma (1,167)
- misc (2)
- project euler (2)
- puzzle (42)
- site news (45)
- tantalizer (45)
- teaser (3)

### Site Stats

- 180,599 hits

Advertisements

The following Python program runs in 740ms (under PyPy).

Solution:The order is P O G B T C R A.Here’s another way of looking at it: start with the spelling out order and see if it matches a feasible initial order

The puzzle (without the specific constraints on the card order) is equivalent to question 1002 posed in Mathematics Magazine, Vol. 49, No. 5 (Nov 1976), p. 253, Mathematical Association of America, http://www.jstor.org/stable/2689460, which asked:

1002. a. For which values of n is it possible to find a permutation of [0, 1,. .., n-1] so that the partial sums, when reduced modulo n, are also a permutation of [0, 1,. .., n-1]? [Bernardo Recamán, University of Warwick.]

b.* Find the number of permutations of [0, 1,. .., n – 1] for n ≤ 12 which solve part a. Can a general formula for the number of solutions be found? [John Hoyt, Indiana University of Pennsylvania.]

The answer to part a. was given in Mathematics Magazine, Vol. 51, No. 4 (Sep 1978), p. 247, http://www.jstor.org/stable/2689475. the answer being that even values of n can have such a permutation. The editor notes that part b. was not answered, but that there are 24 permutations for n=8, found with the aid of a computer.

The answer given does not help in identifying the possible permutations, but all such permutations must have 0 as the first element (i.e. 0 is permuted to itself). Using this together with the constraints of the puzzle, we can rewrite the code as:

A good bit of research, Arthur.

In my original Perl program to solve this I noted that the lengths of the colours where 0-7 (modulo 8), and I treated the pile as a circular buffer with a pointer, rather than re-arranging the pile itself. Although I do remember writing out the initials of the colours on pieces of paper to satisfy myself that the solution was indeed correct.

In my Python program above I somewhat mechanically manipulate the pile in the way described in the puzzle to attempt to give code that is more obviously solving the puzzle.

A more efficient way though is to solve the puzzle backwards, and reconstruct the initial pile starting from the last card turned over (which we are helpfully told is “Apricot”, and we know how many cards must have been moved before we got to it).

The following recursive algorithm runs in 32ms.

The script below in Matlab was used to find the solution (then submitted) to the puzzle when it was originally posed.