Skip to content

Instantly share code, notes, and snippets.

@calroc
Last active April 8, 2020 22:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save calroc/603ed919bc814ccee10c1b3df6142fec to your computer and use it in GitHub Desktop.
Save calroc/603ed919bc814ccee10c1b3df6142fec to your computer and use it in GitHub Desktop.
/*
Via https://www.metalevel.at/prolog/puzzles
The "Zebra Puzzle" https://en.wikipedia.org/wiki/Zebra_Puzzle
- There are five houses.
- The Englishman lives in the red house.
- The Spaniard owns the dog.
- Coffee is drunk in the green house.
- The Ukrainian drinks tea.
- The green house is immediately to the right of the ivory house.
- The Old Gold smoker owns snails.
- Kools are smoked in the yellow house.
- Milk is drunk in the middle house.
- The Norwegian lives in the first house.
- The man who smokes Chesterfields lives in the house next to the man with the fox.
- Kools are smoked in the house next to the house where the horse is kept.
- The Lucky Strike smoker drinks orange juice.
- The Japanese smokes Parliaments.
- The Norwegian lives next to the blue house.
Now, who drinks water? Who owns the zebra?
In the interest of clarity, it must be added that each of the five houses is painted
a different color, and their inhabitants are of different national
extractions, own different pets, drink different beverages and smoke
different brands of American cigarets [sic].
One other thing: in statement 6, right means your right.
*/
:- use_module(library(clpfd)).
house_next_to(H0, H1) :- abs(H0 - H1) #= 1.
/* Thanks https://www.metalevel.at/prolog/puzzles */
puzzle(Vars) :-
Vars = [GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
Dog, Snails, Fox, Horse, Zebra,
Coffee, Tea, Milk, OrangeJuice, Water,
OldGold, Kools, Chesterfields, LuckyStrike, Parliaments
],
Vars ins 1..5,
all_distinct([GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse]),
all_distinct([Englishman, Spaniard, Ukrainian, Norwegian, Japanese]),
all_distinct([Dog, Snails, Fox, Horse, Zebra]),
all_distinct([Coffee, Tea, Milk, OrangeJuice, Water]),
all_distinct([OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
Englishman #= RedHouse,
Spaniard #= Dog,
Coffee #= GreenHouse,
Ukrainian #= Tea,
GreenHouse #= 1 + IvoryHouse,
OldGold #= Snails,
Kools #= YellowHouse,
Milk #= 3,
Norwegian #= 1,
house_next_to(Chesterfields, Fox),
house_next_to(Kools, Horse),
LuckyStrike #= OrangeJuice,
Japanese #= Parliaments,
house_next_to(Norwegian, BlueHouse).
/*
Then to print out the solution after loading the program above use:
puzzle([
GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
Dog, Snails, Fox, Horse, Zebra,
Coffee, Tea, Milk, OrangeJuice, Water,
OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
label([
GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
Dog, Snails, Fox, Horse, Zebra,
Coffee, Tea, Milk, OrangeJuice, Water,
OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]).
*/
@calroc
Copy link
Author

calroc commented Apr 7, 2017

To print out just the values of the Vars you can use:

puzzle(Vs), label(Vs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment