Home More Samples
```///////////////////////////////////////////////////////////////////////////////
//
// Title: Stablemates
// Author: Chris Perton
// Publication: Dell Logic Puzzles
// Issue: April, 1998
// Page: 20
// Stars: 3
//
// Last Saturday, all six of the horses in Johnson Stables were rented to
// children for the day, all of them regulars. Each of the horses (whose names
// are Boris, Lady, Ranger, Santa Fe, Smokey and Topper) lives in one of the
// six stalls, numbered one to six from west to east. The children included
// three boys (Brian, Curtis, and Roy) and three girls (Lily, Michelle, and
// Theresa), each a different age (15, 14, 12, 10, 9, and 7 years old).
//
// The following facts are known about the horses and the children who rode
// them that day:
//
//   1. Topper lives two or more stalls to the east of Theresa's horse.
//   2. The nine-year-old's horse lives somewhere to the west of Brian's horse.
//   3. Three horses in consecutive stalls, from west to east, are Boris, Brian's
//      horse, and the 12-year-old's horse.
//   4. The child who rode Topper is three years older than the one who rode the
//      horse in stall 4, while Roy is three years older than Michelle. (These
//      are 4 different children).
//   5. Ranger's rider is three years older than Lily, who in turn is two years
//      older than the girl who rode Lady.
//   6. Santa Fe lives somewhere to the west of Curtis's horse.
//   7. Brian is just one year older than Theresa.
//   8. Roy didn't ride the horse in stall 6.
//
// Can you determine each horse's stall number, and the name and age of the
// child who rode him or her that day?
//
///////////////////////////////////////////////////////////////////////////////
//
// Running the query
//
//      all Stablemates(stall,horse,age)
//
// will generate the following single solution:
//
// stall = [ {Boris} 2, {Smokey} 5, {Lady} 6, {Ranger} 4, {Santa_Fe} 1,
//           {Topper} 3]
// horse = [ {Brian} Topper, {Curtis} Ranger, {Roy} Smokey, {Lily} Boris,
// age   = [ {Brian} 15, {Curtis} 12, {Roy} 10, {Lily} 9, {Michelle} 7,
//           {Theresa} 14]
//
///////////////////////////////////////////////////////////////////////////////

HorseName = Boris | Smokey | Lady | Ranger | Santa_Fe | Topper
Stall = HorseName->>[1..6]
KidName = Brian | Curtis | Roy | Lily | Michelle | Theresa
Horse = KidName->>HorseName
Age   = KidName->I[7..15]

pred Stablemates(stall::Stall, horse::Horse, age::Age) iff
// Inialize the ages array
age(_) = 15 & age(_) = 14 & age(_) = 12 & age(_) = 10 & age(_) = 9 & age(_) = 7 &

// Transcribe the clues, stalls are enumerated west to east (1 to 6)
{1} stall(Topper) >= stall(horse(Theresa)) + 2 &
{2} age(kid_who_is_nine) = 9 & stall(horse(kid_who_is_nine)) < stall(horse(Brian)) &
{3} stall(Boris) = stall(horse(Brian)) - 1 &
age(kid_who_is_twelve) = 12 & stall(horse(Brian)) = stall(horse(kid_who_is_twelve)) - 1 &
{4} horse(kid_who_rode_topper) = Topper & stall(horse_in_stall_four) = 4 &
horse(kid_who_rode_horse_in_stall_four) = horse_in_stall_four &
age(kid_who_rode_topper) = age(kid_who_rode_horse_in_stall_four) + 3 &
kid_who_rode_topper <> Roy & kid_who_rode_topper <> Michelle &
age(Roy) = age(Michelle) + 3 &
{5} horse(kid_who_rode_ranger) = Ranger & age(kid_who_rode_ranger) = age(Lily) + 3 &