Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Created June 1, 2021 08:20
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/b52ed6d79b8e1a3e0373c72175868df4 to your computer and use it in GitHub Desktop.
Save Pagliacii/b52ed6d79b8e1a3e0373c72175868df4 to your computer and use it in GitHub Desktop.
The following puzzle takes from Dinesman's Superior Mathematical Puzzles in 1968.
/* Where does everyone live?
*
* 1. Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors.
* 2. Baker does not live on the top floor.
* 3. Cooper does not live on the bottom floor.
* 4. Fletcher does not live on either the top or the bottom floor.
* 5. Miller lives on a higher floor than does Cooper.
* 6. Smith does not live on a floor adjacent to Fletcher's.
* 7. Fletcher does not live on a floor adjacent to Cooper's.
*/
higher(A, B, Ls) :-
nth0(M, Ls, A),
nth0(N, Ls, B),
M > N.
adjacent(A, B, Ls) :-
append(_, [A, B | _], Ls);
append(_, [B, A | _], Ls).
floors(Fs) :-
length(Fs, 5), member(baker, Fs), member(cooper, Fs), member(fletcher, Fs), member(miller, Fs), member(smith, Fs), % 1
\+ nth0(4, Fs, baker), % 2
\+ nth0(0, Fs, cooper), % 3
\+ nth0(4, Fs, fletcher), \+ nth0(0, Fs, fletcher), % 4
higher(miller, cooper, Fs), % 5
\+ adjacent(smith, fletcher, Fs), % 6
\+ adjacent(fletcher, cooper, Fs). % 7
@Pagliacii
Copy link
Author

Answer
$ swipl floors_puzzle.pl

?- floors(Fs).
Fs = [smith, cooper, baker, fletcher, miller] .

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