Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Last active June 2, 2021 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pagliacii/330a191818dbb00b018996905a74c445 to your computer and use it in GitHub Desktop.
Save Pagliacii/330a191818dbb00b018996905a74c445 to your computer and use it in GitHub Desktop.
Solve the following “Liars” puzzle from The Sphinx Problem Book by Phillips Hubert in 1934.
/* What in fact was the order in which the five girls were placed?
*
* Five schoolgirls sat for an examination. Their parents--so they thought--showed an undue degree of interest
* in the result. They therefore agreed that, in writing home about the examination, each girl should make one
* true statement and one untrue one. The following are the relevant passages from their letters:
*
* 1. Betty: "Kitty was second in the examination. I was only third."
* 2. Ethel: "You'll be glad to hear that I was on top. Joan was 2nd."
* 3. Joan: "I was third, and poor old Ethel was bottom."
* 4. Kitty: I came out second. Mary was only fourth."
* 5. Mary: "I was fourth. Top place was taken by Betty."
*/
xor(A, B) :-
A, not(B);
B, not(A).
liars(Ls) :-
length(Ls, 5),
member(betty, Ls), member(ethel, Ls), member(joan, Ls), member(kitty, Ls), member(mary, Ls),
xor(nth1(2, Ls, kitty), nth1(3, Ls, betty)),
xor(nth1(1, Ls, ethel), nth1(2, Ls, joan)),
xor(nth1(3, Ls, joan), nth1(5, Ls, ethel)),
xor(nth1(2, Ls, kitty), nth1(4, Ls, mary)),
xor(nth1(4, Ls, mary), nth1(1, Ls, betty)).
@Pagliacii
Copy link
Author

Pagliacii commented Jun 2, 2021

Answer
$ swipl liars-puzzle.pl

?- liars(Ls).
Ls = [kitty, joan, betty, mary, ethel] ;
false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment