Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Created January 16, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yi-jiayu/b6f1c40a6d0f3b14eb3e677763ecf887 to your computer and use it in GitHub Desktop.
Save yi-jiayu/b6f1c40a6d0f3b14eb3e677763ecf887 to your computer and use it in GitHub Desktop.
Prolog solution to the Zebra Puzzle (https://en.wikipedia.org/wiki/Zebra_Puzzle)
:- use_module(library(clpfd)).
zebra(Colors, Nationalities, Pets, Beverages, Cigarettes) :-
Colors = [Red, Green, Ivory, Yellow, Blue],
Nationalities = [English, Spanish, Ukrainian, Norweigian, Japanese],
Pets = [Dog, Snails, Fox, Horse, Zebra],
Beverages = [Coffee, Tea, Milk, Orange, Water],
Cigarettes = [Old, Kools, Chesterfields, Lucky, Parliaments],
Colors ins 1..5,
Nationalities ins 1..5,
Pets ins 1..5,
Beverages ins 1..5,
Cigarettes ins 1..5,
all_different(Colors),
all_different(Nationalities),
all_different(Pets),
all_different(Beverages),
all_different(Cigarettes),
English #= Red,
Spanish #= Dog,
Coffee #= Green,
Ukrainian #= Tea,
Ivory + 1 #= Green,
Old #= Snails,
Kools #= Yellow,
Milk #= 3,
Norweigian #= 1,
Chesterfields #= Fox - 1 #\/ Chesterfields #= Fox + 1,
Kools #= Horse -1 #\/ Kools #= Horse + 1,
Lucky #= Orange,
Japanese #= Parliaments,
Norweigian #= Blue - 1 #\/ Norweigian #= Blue + 1.
% zebra(Colors, Nationalities, Pets, Beverages, Cigarettes), label(Pets), label(Beverages).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment