# Enigmatic Code

Programming Enigma Puzzles

## Enigma 177: Mud in your eye

From New Scientist #1322, 9th September 1982 [link]

All five horses fell at the water jump, reports George Girth, who was riding one of them. In any event, all the jockeys fell off and then, confused by the mud, all got back on to the wrong horse.

On they galloped. Bert Bridle was third past the post, riding the horse belonging to the jockey then riding Frank Fetlock’s horse. Bert’s own horse was being ridden by the jockey who had started on the horse now being ridden by Sam Stirrup. Sam’s own horse (the one he had started on) was beaten home by the horse which Frank Fetlock finished on. Willie Withers and exactly two of the others came in ahead of the horses they had started on.

Can you list the jockeys (capital letters) and their final mounts (small letters naming the original jockey; e.g. Bg would mean B finished on G’s horse) in finishing order?

[enigma177]

Advertisements

### 2 responses to “Enigma 177: Mud in your eye”

1. Jim Randell 19 March 2014 at 8:14 am

This Python program runs in 41ms

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

# labels for riders and horses
labels = (B, F, G, S, W) = "BFGSW"

# assign riders in finishing order
for rider in permutations(labels):

# "B was third past the post..."
if not(rider[2] == B): continue

# assign horses in finishing order
for horse in permutations(labels):

# no rider is on their own horse
if any(r == h for (r, h) in zip(rider, horse)): continue

# "B was third past the post riding the horse belonging to the
# jockey then riding F's horse"
if not(horse[2] == rider[horse.index(F)]): continue

# "B's horse was being ridden by the jockey who had started on the
# horse now being ridden by S"
if not(rider[horse.index(B)] == horse[rider.index(S)]): continue

# "S's own horse was beaten home by the horse F finished on"
if not(rider.index(F) < horse.index(S)): continue

# "W and exactly two of the others came in ahead of the horses
# they started on"
x = list(x for x in labels if rider.index(x) < horse.index(x))
if not(W in x and len(x) == 3): continue

# output the finishing order
printf("finishing order = {r}", r=', '.join(r + h.lower() for (r, h) in zip(rider, horse)))
```

Solution: The finishing order is:

1st – Fb (Frank riding Bert’s horse),
2nd – Sf (Sam riding Frank’s horse),
3rd – Bs (Bert riding Sam’s horse),
4th – Wg (Willie riding George’s horse),
5th – Gw (George riding Willie’s horse).

2. arthurvause 20 March 2014 at 8:54 am

Another one for the “who owns the zebra” template:

```from itertools import permutations as perm

def dict_perms( category_list ):
for p in perm(range(1,6)): yield {a:b for (a,b) in zip(category_list,p)}

def key(d, v): # find a key in dictionary d with value v
return [name for name, val in d.items() if val == v][0]

initals=['B','F','G','S','W']
jockeys = [j for j in dict_perms(initals)
if j['B']==3]

jockeys_horses = [ (jockey,horse) for horse in dict_perms(initals)
for jockey in jockeys
if jockey['B']==3
and key(horse, jockey['B'])==key(jockey,horse['F'])
and key(horse, jockey['S'])==key(jockey,horse['B'])
and horse['S'] > jockey['F']
and jockey['W'] < horse['W']
and sum([1 for x in initals if jockey[x] < horse[x]])==3
]

if len(jockeys_horses) != 1:
print len(jockeys_horses), "solutions"
else:
(jockey,horse) = jockeys_horses[0]
for pos in range(1,6):
print pos, ":", key(jockey,pos), ",",key(horse,pos).lower()
```