Reputation: 9
I need to solve this problem with a Prolog code in SWISH:
" In a quiet street located in a galaxy far, far away, there are five houses, each of a different color. In each house lives a person of a different nationality. Each of the five people drinks a different beverage, has a different type of fish in their aquarium, and has a different pet. Consider the following conditions:
a. The Norwegian lives in the first house.
b. The Englishman lives in the red house.
c. The green house is immediately to the left of the white house.
d. The Danish person drinks tea.
e. The person who drinks coffee lives in the green house.
f. The person who drinks milk lives in the third house.
g. The Swede has a dog as a pet.
h. The Norwegian lives next to the blue house.
i. The German drinks beer.
j. The Brazilian lives in the yellow house.
k. The cat lives in the house next to the one where they drink water.
l. Betta fish live in the house next to the one where they drink tea.
m. The yellow house is next to the house where betta fish live.Your task will be, using Prolog, to determine who has a shark in their aquarium."
I tried this:
color(House, Color) :-
member(House-Color, [
house1-red,
house2-green,
house3-white,
house4-yellow,
house5-blue
]).
nationality(Person, Nationality) :-
member(Person-Nationality, [
house1-norwegian,
house2-english,
house3-danish,
house4-german,
house5-brazilian
]).
drink(Person, Beverage) :-
member(Person-Beverage, [
house1-water,
house2-coffee,
house3-milk,
house4-beer,
house5-tea
]).
fish(House, Fish) :-
member(House-Fish, [
house1-cat,
house2-bird,
house3-betta_fish,
house4-dog,
house5-shark
]).
next_to(X, Y, List) :-
nextto(X, Y, List);
nextto(Y, X, List).
member(X, [X | _]).
member(X, [_ | Rest]) :- member(X, Rest).
solve(Nationality) :-
Houses = [
house(_, _, _, _, _),
house(_, _, _, _, _),
house(_, _, _, _, _),
house(_, _, _, _, _),
house(_, _, _, _, _)
],
member(house(norwegian, _, _, _, _), Houses),
member(house(_, red, _, _, _), Houses),
next_to(house(_, green, _, _, _), house(_, white, _, _, _), Houses),
member(house(danish, _, tea, _, _), Houses),
member(house(_, green, coffee, _, _), Houses),
Houses = [_, _, house(_, _, milk, _, _), _, _],
member(house(swede, _, _, dog, _), Houses),
next_to(house(norwegian, _, _, _, _), house(_, blue, _, _, _), Houses),
member(house(german, _, beer, _, _), Houses),
member(house(brazilian, yellow, _, _, _), Houses),
next_to(house(_, _, _, _, cat), house(_, _, water, _, _), Houses),
next_to(house(_, _, _, _, betta_fish), house(_, _, tea, _, _), Houses),
next_to(house(_, yellow, _, _, _), house(_, _, _, _, betta_fish), Houses),
member(house(_, _, _, shark, _), Houses),
(member(house(_, _, _, cat, _), Houses); member(house(_, _, _, dog, _), Houses)),
member(house(Nationality, _, _, _, shark), Houses).
?- solve(Nationality).
I think the answer is German, but the code gives me Brazilian
Upvotes: 0
Views: 72