Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,82 @@
|
|||
:- use_module(library(clpfd)).
|
||||
|
||||
:- dynamic top/1, bottom/1.
|
||||
|
||||
% Baker does not live on the top floor
|
||||
rule1(L) :-
|
||||
member((baker, F), L),
|
||||
top(Top),
|
||||
F #\= Top.
|
||||
|
||||
% Cooper does not live on the bottom floor.
|
||||
rule2(L) :-
|
||||
member((cooper, F), L),
|
||||
bottom(Bottom),
|
||||
F #\= Bottom.
|
||||
|
||||
% Fletcher does not live on either the top or the bottom floor.
|
||||
rule3(L) :-
|
||||
member((fletcher, F), L),
|
||||
top(Top),
|
||||
bottom(Bottom),
|
||||
F #\= Top,
|
||||
F #\= Bottom.
|
||||
|
||||
% Miller lives on a higher floor than does Cooper.
|
||||
rule4(L) :-
|
||||
member((miller, Fm), L),
|
||||
member((cooper, Fc), L),
|
||||
Fm #> Fc.
|
||||
|
||||
% Smith does not live on a floor adjacent to Fletcher's.
|
||||
rule5(L) :-
|
||||
member((smith, Fs), L),
|
||||
member((fletcher, Ff), L),
|
||||
abs(Fs-Ff) #> 1.
|
||||
|
||||
% Fletcher does not live on a floor adjacent to Cooper's.
|
||||
rule6(L) :-
|
||||
member((cooper, Fc), L),
|
||||
member((fletcher, Ff), L),
|
||||
abs(Fc-Ff) #> 1.
|
||||
|
||||
init(L) :-
|
||||
% we need to define top and bottom
|
||||
assert(bottom(1)),
|
||||
length(L, Top),
|
||||
assert(top(Top)),
|
||||
|
||||
% we say that they are all in differents floors
|
||||
bagof(F, X^member((X, F), L), LF),
|
||||
LF ins 1..Top,
|
||||
all_different(LF),
|
||||
|
||||
% Baker does not live on the top floor
|
||||
rule1(L),
|
||||
|
||||
% Cooper does not live on the bottom floor.
|
||||
rule2(L),
|
||||
|
||||
% Fletcher does not live on either the top or the bottom floor.
|
||||
rule3(L),
|
||||
|
||||
% Miller lives on a higher floor than does Cooper.
|
||||
rule4(L),
|
||||
|
||||
% Smith does not live on a floor adjacent to Fletcher's.
|
||||
rule5(L),
|
||||
|
||||
% Fletcher does not live on a floor adjacent to Cooper's.
|
||||
rule6(L).
|
||||
|
||||
|
||||
solve(L) :-
|
||||
bagof(F, X^member((X, F), L), LF),
|
||||
label(LF).
|
||||
|
||||
dinners :-
|
||||
retractall(top(_)), retractall(bottom(_)),
|
||||
L = [(baker, _Fb), (cooper, _Fc), (fletcher, _Ff), (miller, _Fm), (smith, _Fs)],
|
||||
init(L),
|
||||
solve(L),
|
||||
maplist(writeln, L).
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
select([A|As],S):- select(A,S,S1),select(As,S1).
|
||||
select([],_).
|
||||
|
||||
dinesmans(X) :-
|
||||
%% Baker, Cooper, Fletcher, Miller, and Smith live on different floors
|
||||
%% of an apartment house that contains only five floors.
|
||||
select([Baker,Cooper,Fletcher,Miller,Smith],[1,2,3,4,5]),
|
||||
|
||||
%% Baker does not live on the top floor.
|
||||
Baker =\= 5,
|
||||
|
||||
%% Cooper does not live on the bottom floor.
|
||||
Cooper =\= 1,
|
||||
|
||||
%% Fletcher does not live on either the top or the bottom floor.
|
||||
Fletcher =\= 1, Fletcher =\= 5,
|
||||
|
||||
%% Miller lives on a higher floor than does Cooper.
|
||||
Miller > Cooper,
|
||||
|
||||
%% Smith does not live on a floor adjacent to Fletcher's.
|
||||
1 =\= abs(Smith - Fletcher),
|
||||
|
||||
%% Fletcher does not live on a floor adjacent to Cooper's.
|
||||
1 =\= abs(Fletcher - Cooper),
|
||||
|
||||
%% Where does everyone live?
|
||||
X = ['Baker'(Baker), 'Cooper'(Cooper), 'Fletcher'(Fletcher),
|
||||
'Miller'(Miller), 'Smith'(Smith)].
|
||||
|
||||
main :- bagof( X, dinesmans(X), L )
|
||||
-> maplist( writeln, L), nl, write('No more solutions.')
|
||||
; write('No solutions.').
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
dinesmans(X) :-
|
||||
%% 1. Baker, Cooper, Fletcher, Miller, and Smith live on different floors
|
||||
%% of an apartment house that contains only five floors.
|
||||
Domain = [1,2,3,4,5],
|
||||
|
||||
%% 2. Baker does not live on the top floor.
|
||||
select(Baker,Domain,D1), Baker =\= 5,
|
||||
|
||||
%% 3. Cooper does not live on the bottom floor.
|
||||
select(Cooper,D1,D2), Cooper =\= 1,
|
||||
|
||||
%% 4. Fletcher does not live on either the top or the bottom floor.
|
||||
select(Fletcher,D2,D3), Fletcher =\= 1, Fletcher =\= 5,
|
||||
|
||||
%% 5. Miller lives on a higher floor than does Cooper.
|
||||
select(Miller,D3,D4), Miller > Cooper,
|
||||
|
||||
%% 6. Smith does not live on a floor adjacent to Fletcher's.
|
||||
select(Smith,D4,_), 1 =\= abs(Smith - Fletcher),
|
||||
|
||||
%% 7. Fletcher does not live on a floor adjacent to Cooper's.
|
||||
1 =\= abs(Fletcher - Cooper),
|
||||
|
||||
%% Where does everyone live?
|
||||
X = ['Baker'(Baker), 'Cooper'(Cooper), 'Fletcher'(Fletcher),
|
||||
'Miller'(Miller), 'Smith'(Smith)].
|
||||
Loading…
Add table
Add a link
Reference in a new issue