Created
May 19, 2021 13:05
-
-
Save Pagliacii/44151a821ca76dd7940e6cc283dd5ad0 to your computer and use it in GitHub Desktop.
Who owns the zebra and who drinks water?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Houses logical puzzle: who owns the zebra and who drinks water? | |
* | |
* 1. Five colored houses in a row, each with an owner, a pet, cigarettes, and a drink. | |
* 2. The English lives in the red house. | |
* 3. The Spanish has a dog. | |
* 4. They drink coffee in the green house. | |
* 5. The Ukrainian drinks tea. | |
* 6. The green house is next to the white house. | |
* 7. The Winston smoker has a serpent. | |
* 8. In the yellow house they smoke Kool. | |
* 9. In the middle house they drink milk. | |
* 10. The Norwegian lives in the first house. | |
* 11. The Chesterfield smoker lives near the man with the fox. | |
* 12. In the house near the house with the horse they smoke Kool. | |
* 13. The Lucky Strike smoker drinks juice. | |
* 14. The Japanese smokes Kent. | |
* 15. The Norwegian lives near the blue house. | |
*/ | |
zebra_owner(Owner) :- | |
houses(Hs), | |
member(h(Owner, zebra, _, _, _), Hs). | |
water_drinker(Drinker) :- | |
houses(Hs), | |
member(h(Drinker, _, _, water, _), Hs). | |
next(A, B, Ls) :- | |
nth0(M, Ls, A), | |
N is M+1, | |
nth0(N, Ls, C), | |
C = B. | |
near(A, B, Ls) :- | |
next(A, B, Ls); | |
next(B, A, Ls). | |
houses(Hs) :- | |
% each house in the list Hs of houses is represented as: | |
% h(Nationality, Pet, Cigarette, Drink, Color) | |
length(Hs, 5), % 1 | |
member(h(english, _, _, _, red), Hs), % 2 | |
member(h(spanish, dog, _, _, _), Hs), % 3 | |
member(h(_, _, _, coffee, green), Hs), % 4 | |
member(h(ukrainian, _, _, tea, _), Hs), % 5 | |
next(h(_, _, _, _, white), h(_, _, _, _, green), Hs), % 6 | |
member(h(_, serpent, winston, _, _), Hs), % 7 | |
member(h(_, _, kool, _, yellow), Hs), % 8 | |
nth0(2, Hs, h(_, _, _, milk, _)), % 9 | |
nth0(0, Hs, h(norwegian, _, _, _, _)), % 10 | |
near(h(_, _, chesterfield, _, _), h(_, fox, _, _, _), Hs), % 11 | |
near(h(_, _, kool, _, _), h(_, house, _, _, _), Hs), % 12 | |
member(h(_, _, lucky, juice, _), Hs), % 13 | |
member(h(japanese, _, kent, _, _), Hs), % 14 | |
near(h(norwegian, _, _, _, _), h(_, _, _, _, blue), Hs), % 15 | |
member(h(_, _, _, water, _), Hs), % one of them drinks water | |
member(h(_, zebra, _, _, _), Hs). % one of them owns a zebra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved
next
:Answer