Edit: this puzzle is also known as "Einstein's Riddle"
The Who owns the Zebra (you can try the online version here) is an example of a classic set of puzzles and I bet that most people on Stack Overflow can solve it with pen and paper. But what would a programmatic solution look like?
Based on the clues listed below...
- There are five houses.
- Each house has its own unique color.
- All house owners are of different nationalities.
- They all have different pets.
- They all drink different drinks.
- They all smoke different cigarettes.
- The English man lives in the red house.
- The Swede has a dog.
- The Dane drinks tea.
- The green house is on the left side of the white house.
- They drink coffee in the green house.
- The man who smokes Pall Mall has birds.
- In the yellow house they smoke Dunhill.
- In the middle house they drink milk.
- The Norwegian lives in the first house.
- The man who smokes Blend lives in the house next to the house with cats.
- In the house next to the house where they have a horse, they smoke Dunhill.
- The man who smokes Blue Master drinks beer.
- The German smokes Prince.
- The Norwegian lives next to the blue house.
- They drink water in the house next to the house where they smoke Blend.
...who owns the Zebra?
This is a MiniZinc solution to the zebra puzzle as defined in Wikipedia:
Solution:
Another Python solution, this time using Python's PyKE (Python Knowledge Engine). Granted, it's more verbose than using Python's "constraint" module in the solution by @J.F.Sebastian, but it provides an interesting comparison for anybody looking into a raw knowledge engine for this type of problem.
clues.kfb
relations.krb
driver.py (actually larger, but this is the essence)
Sample output:
Source: https://github.com/DreadPirateShawn/pyke-who-owns-zebra