# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1320: Around the tree

From New Scientist #2478, 18th December 2004

Six gifts are arranged in a circle around the Christmas tree. There is one each for Gail, Simon, Rachel, Bob, Yvonne and Will. The six wrappings are green, silver, red, blue, yellow and white. The six ribbons are also those colours. But if you look at any two adjacent gifts then the initials of their recipients, the colours of their wrappings and the colour of their ribbons give six different letters.

The white-wrapped gift is between the yellow-wrapped and the green-wrapped ones. The next gift clockwise from Rachel’s has a ribbon of the same colour (not green) as the wrapping of Yvonne’s gift. The next gift clockwise from the one with green ribbon has a wrapper the same colour as the ribbon on the white-wrapped gift. The next gift clockwise from the blue-wrapped gift has a ribbon which is the same colour as the wrapping on Simon’s gift.

List the recipients of the gifts in clockwise order starting with Rachel.

Enigma 1423 is also called “Around the tree”.

[enigma1320]

### One response to “Enigma 1320: Around the tree”

1. Jim Randell 1 June 2014 at 8:33 am

This Python program runs in 41ms.

```from itertools import permutations
from enigma import printf

# labels used for names, wrappings and ribbons
labels = set('GSRBYW')

# ns - sequence of names
# ws - sequence of wrappings
# rs - sequence of ribbons
def solve(ns, ws, rs):
# are we done?
if len(ns) == 6:
# check the conditions
# "the W-wrapped gift is between the Y-wrapped and the G-wrapped ones"
wW = ws.index('W')
if set((ws[(wW - 1) % 6], ws[(wW + 1) % 6])) != set('YG'): return
# "the next gift clockwise from R has a ribbon of the same colour
# (not G) as the wrapping of Y's gift"
nY = ns.index('Y')
if rs[1] == 'G' or rs[1] != ws[nY]: return
# "the next gift clockwise from the one with G ribbon has a
# wrapper the same colour as the ribbon on the W-wrapped gift"
rG = rs.index('G')
if ws[(rG + 1) % 6] != rs[wW]: return
# "the next gift clockwise from the B-wrapped gift has a ribbon
# which is the same colour as the wrapping on S's gift"
wB = ws.index('B')
nS = ns.index('S')
if rs[(wB + 1) % 6] != ws[nS]: return
printf("ns={ns} [ws={ws} rs={rs}]", ns=','.join(ns), ws=','.join(ws), rs=','.join(rs))
else:
for (n, w, r) in permutations(labels.difference((ns[-1], ws[-1], rs[-1])), 3):
if n in ns or w in ws or r in rs: continue
solve(ns + [n], ws + [w], rs + [r])