Home More Samples
```///////////////////////////////////////////////////////////////////////////////
//
// Title: Sleeping Arrangements
// Author: Scott Marley
// Publication: Dell Logic Puzzles
// Issue: April, 1998
// Page: 13
// Stars: 2
//
// The Dillies have five teenaged children, two boys named Ollie and Rollie,
// and three girls named Mellie, Nellie, and Pollie. Each is a different number
// of years old, from 13 to 17. There are three bedrooms for the children in
// the Dillie house, so two share the yellow room, two share the white room,
// and one alone has the smaller green room. Can you match each one's name
// and age, and tell who sleeps where?
//
// 1. No one shares a room with a sibling of the opposite sex.
// 2. Pollie is exactly one year older than Mellie.
// 3. The two teenagers who share the yellow room are two years apart in age.
// 4. The two who share the white room are three years apart in age.
// 5. Rollie is somewhat older than Ollie, but somewhat younger than the
//    sibling who has the green room.
//
///////////////////////////////////////////////////////////////////////////////
//
// Query:
//
//      all SleepingArrangements(age, rooms)
//
///////////////////////////////////////////////////////////////////////////////
//
// Result:
//
//    age   = [ {Ollie} 13, {Rollie} 15, {Mellie} 16, {Nellie} 14, {Pollie} 17]
//    rooms = [ {Ollie} Yellow, {Rollie} Yellow, {Mellie} Green,
//              {Nellie} White, {Pollie} White]
//
///////////////////////////////////////////////////////////////////////////////
//
// Note: In the code below, "No one shares a room with a sibling of the
//      opposite sex" we issued a constraint
//
//              w1 > w2
//
//
//              w1 <> w2
//
//      This is a fairly common way to eliminate multiple identical
//      solutions. Basically, we fixed the order of the White room occupants
//      (we don't care if the White room is occupied by Nellie and Pollie,
//      or Pollie and Nellie).
//      It also makes some following constraints easier to write.
//      "The two who share the white room are three years apart in age".
//      can be now written as
//
//               age(w1) = age(w2) + 3
//
//
//              (age(w1) = age(w2) + 3) | (age(w2) = age(w1) + 3)
//
//
//      Same reasoning can be applied to y1, y2 (the occupants of the Yellow
//      room)
//
///////////////////////////////////////////////////////////////////////////////

local Name =  Ollie | Rollie | Mellie | Nellie | Pollie
local RoomColor = Yellow | White | Green
local Age = Name->>[13..17]
local Rooms = Name->RoomColor

pred SleepingArrangements(age::Age, rooms::Rooms) iff
// 1. No one shares a room with a sibling of the opposite sex.
rooms(w1) = White  & rooms(w2) = White  & w1 > w2 & SameSex(w1,w2) &
rooms(y1) = Yellow & rooms(y2) = Yellow & y1 > y2 & SameSex(y1,y2) &

// 2. Pollie is exactly one year older than Mellie.
age(Pollie) = age(Mellie) + 1 &

// 3. The two teenagers who share the yellow room are two years apart in age.
age(y1) = age(y2) + 2 &

// 4. The two who share the white room are three years apart in age.
age(w1) = age(w2) + 3 &

// 5. Rollie is somewhat older than Ollie, but somewhat younger than the
//    sibling who has the green room.
age(Rollie) > age(Ollie) &
age(Rollie) < age(x5) & rooms(x5) = Green

//////////////////////////////////////////////////////////////////////////////
// Some simple helper predicates to keep the code more readable

local pred SameSex(name1::Name,name2::Name) iff
(IsBoy(name1) & IsBoy(name2))|
(IsGirl(name1) & IsGirl(name2))

local pred IsBoy(name::Name) iff
name = Ollie | name = Rollie

local pred IsGirl(name::Name) iff
name = Mellie | name = Nellie | name = Pollie

```