Skip to content

Instantly share code, notes, and snippets.

@vmorsell
Created December 12, 2023 21:51
Show Gist options
  • Save vmorsell/119db7fc65e0af05edb2ae48859647fe to your computer and use it in GitHub Desktop.
Save vmorsell/119db7fc65e0af05edb2ae48859647fe to your computer and use it in GitHub Desktop.
Einstein's Puzzle in Prolog
/*
* Einstein's Puzzle solver
*
* Assignment and clues as in 1962's Life International
* version according to Wikipedia (2023-12-12).
* https://en.wikipedia.org/wiki/Zebra_Puzzle#Description
*/
solve :-
clues(Houses),
member(house(_, WaterDrinker, water, _, _), Houses),
member(house(_, ZebraOwner, _, _, zebra), Houses),
format('The ~w drinks water, and the ~w owns the zebra.~n', [WaterDrinker, ZebraOwner]).
right_of(R, L, [L, R | _]).
right_of(R, L, [_ | Rest]) :- right_of(R, L, Rest).
next_to(X, Y, List) :-
right_of(X, Y, List);
right_of(Y, X, List).
clues(Houses) :-
length(Houses, 5),
member(house(red, 'Englishman', _, _, _), Houses),
member(house(_, 'Spaniard', _, _, dog), Houses),
member(house(green, _, coffee, _, _), Houses),
member(house(_, 'Ukrainian', tea, _, _), Houses),
right_of(house(green, _, _, _, _), house(ivory, _, _, _, _), Houses),
member(house(_, _, _, old_gold, snails), Houses),
member(house(yellow, _, _, kools, _), Houses),
nth1(3, Houses, house(_, _, milk, _, _)),
nth1(1, Houses, house(_, 'Norwegian', _, _, _)),
next_to(house(_, _, _, chesterfield, _), house(_, _, _, _, fox), Houses),
next_to(house(_, _, _, kools, _), house(_, _, _, _, horse), Houses),
member(house(_, _, orange_juice, lucky_strike, _), Houses),
member(house(_, 'Japanese', _, parliament, _), Houses),
next_to(house(_, 'Norwegian', _, _, _), house(blue, _, _, _, _), Houses).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment