Hello, I have a task to write the Einstein riddle.
Friends: Jarek, Franek, Stefan, Albert, Szymon, Robert, Marcin live in one building, each on a different floor (0,1,2,…).
Each of them has a different pet: dog, cat, fish, hamster, parrot, snake, canary.
Which floor does each of them live on, and what kind of pet does it have, if the following statements are true?
- Franek doesn’t have a dog.
- Owner of a dog lives on floor 2.
- Albert doesn’t have a hamster.
- Owner of a hamster lives on floor 0.
- Marcin lives lower than Stefan.
- Owner of a canary lives on floor 6.
- Robert doesn’t have a snake.
- Owner of a snake lives on floor 3.
- Franek lives lower than Albert.
- Franek lives higher than Robert.
- Owner of a cat lives on floor 5.
- Owner of a parrot lives on floor 1.
- Szymon lives higher than Stefan.
- Marcin lives lower than Szymon.
- Marcin lives lower than Jarek.
- Owner of a fish lives on floor 4.
- Marcin lives lower than Robert.
- Stefan doesn’t have a canary.
- Stefan lives lower than Albert.
- Albert lives higher than Robert.
- Robert doesn’t have a parrot.
- Stefan doesn’t have a cat.
- Szymon lives higher than Franek.
- Albert lives higher than Marcin.
- Szymon doesn’t have a hamster.
- Jarek doesn’t have a snake.
- Franek lives higher than Marcin.
- Stefan lives lower than Franek.
- Szymon doesn’t have a dog.
- Marcin doesn’t a canary.
- Jarek doesn’t have a dog.
- Jarek doesn’t have a fish.
- Albert doesn’t have a dog.
- Albert lives higher than Szymon.
- Szymon doesn’t have a parrot.
- Szymon lives higher than Robert.
- Robert lives higher than Jarek.
- Marcin doesn’t have a snake.
- Jarek lives lower than Albert.
- Jarek lives lower than Franek.
- Stefan lives higher than Jarek.
- Szymon lives higher than Jarek.
- Robert doesn’t have a fish.
- Robert lives lower than Stefan.
- Albert doesn’t have a snake.
- Robert doesn’t have a cat…
- Jarek doesn’t have a canary.
- Marcin doesn’t have a fish.
- Jarek doesn’t have a cat
- Stefan doesn’t have a parrot.
- Albert doesn’t have a cat.
- Stefan doesn’t have a hamster.
- Franek doesn’t have a canary.
- Szymon doesn’t have a snake.
- Stefan doesn’t have a dog.
- Stefan doesn’t have a fish.
- Robert doesn’t have a hamster.
- Albert doesn’t have a fish.
- Robert doesn’t have a canary.
- Jarek doesn’t have a hamster.
- Franek doesn’t have a snake.
- Szymon doesn’t have a canary.
- Franek doesn’t have a parrot.
- Albert doesn’t have a parrot.
- Marcin doesn’t have a dog.
- Szymon doesn’t have a fish.
The problem is when i write
?- puzzle(Table)
the answer is
false
This is wrong, because i conclude that, for example, Robert has a dog, so lives on floor 2.
I don’t know, where is the problem.
Here is my code.
puzzle(Table) :-
Table = [row(_, 0, hamster), % Owner of a hamster lives on floor 0.
row(_, 1, papuga), % Owner of a parrot lives on floor 1.
row(_, 2, pies), % Owner of a dog lives on floor 2.
row(_, 3, waz), % Owner of a snake lives on floor 3.
row(_, 4, rybka), % Owner of a fish lives on floor 4.
row(_, 5, kot), % Owner of a cat lives on floor 5.
row(_, 6, kanarek)], % Owner of a canary lives on floor 6.
memberchk(row('Franek', LFranek, AFranek), Table),
memberchk(row('Albert', LAlbert, AAlbert), Table),
memberchk(row('Robert', LRobert, ARobert), Table),
memberchk(row('Stefan', LStefan, AStefan), Table),
memberchk(row('Simon', LSzymon, ASzymon), Table),
memberchk(row('Jarek', LJarek, AJarek), Table),
memberchk(row('Marcin', LMarcin, AMarcin), Table),
% Franek doesn't have a dog.
% Franek doesn't a parrot.
% Franek doesn't a snake.
% Franek doesn't a canary.
\+ memberchk(AFranek, [dog, parrot, snake, canary]),
% Albert no dog
% Albert no cat
% Albert no fish
% Albert no hamster
% Albert no parrot
% Albert no snake
% Albert no canary
\+ memberchk(AAlbert, [dog, cat, fish, hamster, parrot, snake, canary]),
% Robert no cat.
% Robert no fish.
% Robert no hamster.
% Robert no parrot.
% Robert no snake.
% Robert no canary.
\+ memberchk(ARobert, [cat, fish, hamster, parrot, snake, canary]),
% Stefan no dog.
% Stefan no cat.
% Stefan no fish.
% Stefan no hamster.
% Stefan no parrot.
% Stefan no canary.
\+ memberchk(AStefan, [dog, cat, fish, hamster, parrot, canary]),
% Szymon no dog.
% Szymon no fish.
% Szymon no hamster
% Szymon no parrot
% Szymon no snake
% Szymon no canary
\+ memberchk(ASzymon, [dog, fish, hamster, parrot, snake, canary]),
% Jarek no dog
% Jarek no cat
% Jarek no fish
% Jarek no hamster
% Jarek no snake
% Jarek no canary
\+ memberchk(AJarek, [dog, cat, fish, hamster, snake, canary]),
% Marcin no dog
% Marcin no fish
% Marcin no snake
% Marcin no canary
\+ memberchk(AMarcin, [dog, fish, snake, canary]),
LMarcin < LStefan, % 5.
LFranek < LAlbert, % 9.
LFranek > LRobert, % 10.
LSzymon > LStefan, % 13.
LMarcin < LSzymon, % 14.
LMarcin < LJarek, % 15.
LMarcin < LRobert, % 17.
LStefan < LAlbert, % 19.
LAlbert > LRobert, % 20.
LSzymon > LFranek, % 23.
LAlbert > LMarcin, % 24.
LFranek > LMarcin, % 27.
LStefan < LFranek, % 28.
LAlbert > LSzymon, % 34.
LSzymon > LRobert, % 36.
LRobert > LJarek, % 37.
LJarek < LAlbert, % 39.
LJarek < LFranek, % 40.
LStefan > LJarek, % 41.
LSzymon > LJarek, % 42.
LRobert < LStefan. % 44.
Please tell me what’s wrong with it and how it should be done.
Thank you in advance.