Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % assume member_/2 is defined
- solve(Neighborhood) :-
- /*
- the solution consists of five compounds
- each compound consists of the six traits of a house, in order
- position = 1, 2, 3, 4, 5
- nationality = brit, dane, german, norwegian, swede
- colors = blue, green, red, white, yellow
- beverages = beer, coffee, milk, tea, water
- smokes = bluemaster, dunhill, pallmall, prince, blend
- keeps = cat, bird, dog, fish, horse
- */
- % the neighborhood looks like this
- Neighborhood = [
- (1,_,_,_,_,_),
- (2,_,_,_,_,_),
- (3,_,_,_,_,_),
- (4,_,_,_,_,_),
- (5,_,_,_,_,_)],
- maplist(lists:member_(Neighborhood),[
- % 1. The Brit lives in a red house.
- (_,brit,red,_,_,_),
- % 2. The Swede keeps dogs as pets.
- (_,swede,_,_,_,dog),
- % 3. The Dane drinks tea.
- (_,dane,_,tea,_,_),
- % 4. The green house is on the left of the white house (next to it).
- (A,_,green,_,_), B is A+1,
- (B,_,white,_,_),
- % 5. The green house owner drinks coffee.
- (_,_,green,coffee,_,_),
- % 6. The person who smokes Pall Mall rears birds.
- (_,_,_,_,pallmall,birds),
- % 7. The owner of the yellow house smokes Dunhill.
- (_,_,yellow,_,dunhill,_),
- % 8. The man living in the house right in the center drinks milk.
- (3,_,_,milk,_,_),
- % 9. The Norwegian lives in the first house.
- (1,norwegian,_,_,_,_),
- % 10. The man who smokes blend lives next to the one who keeps cats.
- (C,_,_,_,blend,_), plus_or_minus_one(C, D),
- (D,_,_,_,_,cats),
- % 11. The man who keeps horses lives next to the man who smokes Dunhill.
- (E,_,_,_,_,horses), plus_or_minus_one(E, F),
- (F,_,_,_,dunhill,_),
- % 12. The owner who smokes Blue Master drinks beer.
- (_,_,_,beer,bluemaster,_),
- % 13. The German smokes Prince.
- (_,german,_,_,prince,_),
- % 14. The Norwegian lives next to the blue house.
- (G,norwegian,_,_,_,_), plus_or_minus_one(G, H),
- (H,_,blue,_,_,_),
- % 15. The man who smokes blend has a neighbor who drinks water.
- (I,_,_,_,blend,_), plus_or_minus_one(I, J),
- (J,_,_,water,_,_),
- % Question: Who owns the fish?
- (_,_,_,_,_,fish)]).
- % This is probably very very very bad practice
- plus_or_minus_one(I, J) :- Im is I-1, Ip is I+1, member(J, [Im, Ip]).
- main :- solve(Neighborhood), maplist(writeln, Neighborhood).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement