4 ms·
Triska on the front page! Sweet. It was in large part thanks to "The Power of Prolog" that I was able to figure out a nice solution to the "Zebra" puzzle:
by carapace 7y ago
Triska on the front page! Sweet.
It was in large part thanks to "The Power of Prolog" that I was able to figure out a nice solution to the "Zebra" puzzle:
/*
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]).
*/
See also "Boring Sudoku":
/*
Boring Prolog Sudoku.
*/
:- use_module(library(clpfd)).
puzzle(Vars) :-
Vars = [
A11, A12, A13, A14, A15, A16, A17, A18, A19,
A21, A22, A23, A24, A25, A26, A27, A28, A29,
A31, A32, A33, A34, A35, A36, A37, A38, A39,
A41, A42, A43, A44, A45, A46, A47, A48, A49,
A51, A52, A53, A54, A55, A56, A57, A58, A59,
A61, A62, A63, A64, A65, A66, A67, A68, A69,
A71, A72, A73, A74, A75, A76, A77, A78, A79,
A81, A82, A83, A84, A85, A86, A87, A88, A89,
A91, A92, A93, A94, A95, A96, A97, A98, A99],
Vars ins 1..9,
all_distinct([A11, A12, A13, A14, A15, A16, A17, A18, A19]),
all_distinct([A21, A22, A23, A24, A25, A26, A27, A28, A29]),
all_distinct([A31, A32, A33, A34, A35, A36, A37, A38, A39]),
all_distinct([A41, A42, A43, A44, A45, A46, A47, A48, A49]),
all_distinct([A51, A52, A53, A54, A55, A56, A57, A58, A59]),
all_distinct([A61, A62, A63, A64, A65, A66, A67, A68, A69]),
all_distinct([A71, A72, A73, A74, A75, A76, A77, A78, A79]),
all_distinct([A81, A82, A83, A84, A85, A86, A87, A88, A89]),
all_distinct([A91, A92, A93, A94, A95, A96, A97, A98, A99]),
all_distinct([A11, A21, A31, A41, A51, A61, A71, A81, A91]),
all_distinct([A12, A22, A32, A42, A52, A62, A72, A82, A92]),
all_distinct([A13, A23, A33, A43, A53, A63, A73, A83, A93]),
all_distinct([A14, A24, A34, A44, A54, A64, A74, A84, A94]),
all_distinct([A15, A25, A35, A45, A55, A65, A75, A85, A95]),
all_distinct([A16, A26, A36, A46, A56, A66, A76, A86, A96]),
all_distinct([A17, A27, A37, A47, A57, A67, A77, A87, A97]),
all_distinct([A18, A28, A38, A48, A58, A68, A78, A88, A98]),
all_distinct([A19, A29, A39, A49, A59, A69, A79, A89, A99]),
all_distinct([A11, A21, A31, A12, A22, A32, A13, A23, A33]),
all_distinct([A41, A51, A61, A42, A52, A62, A43, A53, A63]),
all_distinct([A71, A81, A91, A72, A82, A92, A73, A83, A93]),
all_distinct([A14, A24, A34, A15, A25, A35, A16, A26, A36]),
all_distinct([A44, A54, A64, A45, A55, A65, A46, A56, A66]),
all_distinct([A74, A84, A94, A75, A85, A95, A76, A86, A96]),
all_distinct([A17, A27, A37, A18, A28, A38, A19, A29, A39]),
all_distinct([A47, A57, A67, A48, A58, A68, A49, A59, A69]),
all_distinct([A77, A87, A97, A78, A88, A98, A79, A89, A99]).
- todd8 7y agoFor comparison, here is my own solution in Python 3 to the Zebra Puzzle. To me this solution seems sorter and easier to understand: from itertools import permutations as perms houses = range(1,6) # houses numbered 1,2,3,4,5 for Englishman, Spaniard, Ukrainian, Norwegian, Japanese in perms(houses): if Norwegian != 1: continue # rule 10 for red, green, ivory, yellow, blue in perms(houses): if Englishman != red: continue # rule 2 if green != ivory + 1: continue # rule 6 if Norwegian not in [blue-1,blue+1]: continue # rule 15 for coffee, tea, milk, oj, water in perms(houses): if coffee != green: continue # rule 4 if Ukrainian != tea: continue # rule 5 if milk != 3: continue # rule 9 for oldgold, kools, chesterfields, luckys, parliaments in perms(houses): if kools != yellow: continue # rule 8 if luckys != oj: continue # rule 13 if Japanese != parliaments: continue # rule 14 for dog, snails, fox, horse, zebra in perms(houses): if Spaniard != dog: continue # rule 3 if oldgold != snails: continue # rule 7 if chesterfields not in [fox-1,fox+1]: continue # rule 11 if kools not in [horse-1, horse+1]: continue # rule 12 person = {Englishman: 'Englishman', Spaniard: 'Spainard', Ukrainian: 'Ukrainian', Norwegian: 'Norwegian', Japanese: 'Japanese'} print(f'{person[water]} drinks water') print(f'{person[zebra]} owns the zebra')
- anewhnaccount2 7y agoEnumerating all solutions and checking will always be the simplest approach for find the set of inputs returning true for a decision problem. However, a constraint solver will not necessarily have to enumerate all solutions.
- todd8 7y agoYes it would take a long time to do it that way since there would be roughly 25 billion solutions to evaluate. My Python program doesn’t do this and runs very fast. Elapsed time for this program is approximately 0.03 seconds on my 2013 desktop and it finds all possible solution (in this case one). Normally, inner loops are executed more times than outer loops, but in this program the bodies of the inner loops are skipped (because of the continue statements) when constraints fail in the the outer loops. The five loop bodies are executed a total of 120, 2880, 1440, 1440, 960 times respectively (I put counters in each loop to determine this, all counters were initialized before any of the loops begin). I wrote this program (for a different version of this same problem) while waiting for my daughter to come downstairs to be driven to school one morning around five years ago. (See https://news.ycombinator.com/item?id=7734998 https://news.ycombinator.com/item?id=7734998)