Hacker News linked to a logic puzzle with the following constraints:
There are five houses in five different colors starting from left to right. In each house lives a person of a different nationality. These owners all drink a certain type of beverage, smoke a certain brand of cigarette and keep a certain type of pet. No two owners have the same pet, smoke the same brand or drink the same beverage. The question is: WHO OWNS THE FISH??? Hints:
- The Brit lives in the red house
- The Swede keeps dogs as pets
- The Dane drinks tea
- The green house is on the left of the white house
- The green house’s owner drinks coffee
- The person who smokes Pall Mall rears birds
- The owner of the yellow house smokes Dunhill
- The man living in the centre house drinks milk
- The Norwegian lives in the first house
- The person who smokes Marlboro lives next to the one who keeps cats
- The person who keeps horses lives next to the person who smokes Dunhill
- The person who smokes Winfield drinks beer
- The German smokes Rothmans
- The Norwegian lives next to the blue house
- The person who smokes Marlboro has a neighbor who drinks water
Alloy is very well-suited to solving this type of problem, so I gave it a go. Here’s what my Alloy model looks like:
open util/ordering[House] sig House { color: one Color } abstract sig Person { occupies: one House, drinks: one Beverage, smokes: one Cigarette, keeps: one Pet } one sig Brit, Swede, Dane, Norwegian, German extends Person {} abstract sig Color {} one sig White, Yellow, Blue, Red, Green extends Color {} abstract sig Beverage {} one sig Tea, Coffee, Milk, Beer, Water extends Beverage {} abstract sig Pet {} one sig Birds, Cats, Dogs, Horses, Fish extends Pet {} abstract sig Cigarette {} one sig PallMall, Dunhill, Marlboro, Winfield, Rothmans extends Cigarette {} fact allRelationsAreOneToOne { color.~color in iden occupies.~occupies in iden drinks.~drinks in iden smokes.~smokes in iden keeps.~keeps in iden } pred problemConstraints { // The Brit lives in the red house Red in Brit.occupies.color //The Swede keeps dogs as pets Dogs in Swede.keeps // The Dane drinks tea Tea in Dane.drinks // The green house is on the left of the white house Green in prev[color.White].color // The green house's owner drinks coffee Coffee in (occupies.(color.Green)).drinks // The person who smokes Pall Mall rears birds Birds in (smokes.PallMall).keeps // The owner of the yellow house smokes Dunhill Dunhill in (occupies.(color.Yellow)).smokes // The man living in the centre house drinks milk (drinks.Milk).occupies in first[].next.next // The Norwegian lives in the first house Norwegian in occupies.first[] // The person who smokes Marlboro lives next to the one who keeps cats (smokes.Marlboro).occupies in (keeps.Cats).occupies.(next + prev) // The person who keeps horses lives next to the person who smokes Dunhill (keeps.Horses).occupies in (smokes.Dunhill).occupies.(next + prev) // The person who smokes Winfield drinks beer Beer in (smokes.Winfield).drinks // The German smokes Rothmans German in smokes.Rothmans // The Norwegian lives next to the blue house Blue in Norwegian.occupies.(next+prev).color // The person who smokes Marlboro has a neigbor who drinks water (drinks.Water).occupies in (smokes.Marlboro).occupies.(next+prev) } run problemConstraints for exactly 5 House
Alloy’s “Magic Layout” did a surprisingly good job at displaying the results. I had to manually rearrange the output so the houses would be displayed in the correct order, but otherwise no fiddling was required. Here’s what it looks like:
I also put it up on Github.
One thought on ““Who owns the fish” in Alloy”