# Enigmatic Code

Programming Enigma Puzzles

## Enigma 386: Triangle of stones

From New Scientist #1535, 20th November 1986 [link]

I emerged from the impenetrable jungle into a clearing, at the centre of which were 21 stones laid on the ground to form a triangle.

Just then, three native girls approached the stones; each wore a coloured sarong, one red, one blue, one white. They painted the six stones in the bottom row, white, white, blue, blue, red, red, in that order from left to right, and placed a coconut on the single stone in the top row. My guide explained that this was a traditional game. The girls would go and turn in the order red, white, blue, red, white. At each turn the girl would move the coconut to a stone which touched the coconut’s present stone and which was on a lower row. The game ended when the coconut reached the bottom row, and the colour of its final stone indicate the winner.

My guide knew the girls and said that if red could not win she would try to help white to win, similarly white would help blue and blue would help red: all the girls knew this.

After the game they exchanged the colours on two of the stones and played again. Blue won the second game.

Who won the first game and what was the row of colours for the second game?

[enigma386]

### One response to “Enigma 386: Triangle of stones”

1. Jim Randell 27 February 2017 at 8:27 am

We can solve the puzzle by playing the game backwards.

White makes the final move to the bottom row. If the coconut is in the leftmost position then it is only possible to move it to a white stone. Likewise if the coconut is on the middle stone it can only be moved to a blue stone, and if it is on the rightmost stone it can only be moved to a red stone.

So, White only has to make a choice if the coconut is in the 2nd or 4th positions (counting from the left). If it’s in the second position (a choice between White and Blue), she chooses White (so that she wins). If it’s in the fourth position (a choice between Blue and Red), she favours Blue.

So we can colour the row of five stones: White, White, Blue, Blue, Red, and then continue on to the row of four stones, by considering the actions of Red.

We continue in this way until we have coloured the topmost stone, and this indicates the winner of the game.

This Python program runs in 42ms.

```from itertools import combinations
from enigma import tuples, update, join, printf

# player preference
pref = {
'R': { 'R': 1, 'W': 2, 'B': 3 },
'W': { 'W': 1, 'B': 2, 'R': 3 },
'B': { 'B': 1, 'R': 2, 'W': 3 },
}

# play the game
# ss = stones, ps = players
def solve(ss, ps):
while ps:
# the next player is...
(p, ps) = (ps[0], ps[1:])
# construct the next layer
ss = list(min(a, b, key=lambda x: pref[p][x]) for (a, b) in tuples(ss, 2))
return ss

# starting bricks
start = list("WWBBRR")

# player order (reversed)
order = list("WRBWR")

# part 1: who wins the first game?
s = solve(start, order)
printf("part 1: winner = {s}", s=join(s, sep=' '))

# part 2: find a game where B wins
# choose bricks i and j to swap
for ((i, a), (j, b)) in combinations(enumerate(start), 2):
# colours must be different
if a == b: continue
# swap the colours on the bricks
s0 = update(start, [(i, b), (j, a)])
# play the game
s = solve(s0, order)
# is the winner B?
if s[0] == 'B':
printf("part 2: start = {s0}", s0=join(s0, sep=' '))
```

Solution: The first game is won by White. The second game started with colours: White, Red, Blue, Blue, Red, White.

The colouring of the stones for the first game is:

And for the second game:

This site uses Akismet to reduce spam. Learn how your comment data is processed.