Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,21 @@
select([A|As],S):- select(A,S,S1),select(As,S1).
select([],_).
next_to(A,B,C):- left_of(A,B,C) ; left_of(B,A,C).
left_of(A,B,C):- append(_,[A,B|_],C).
zebra(Owns, HS):- % color,nation,pet,drink,smokes
HS = [ h(_,norwegian,_,_,_), _, h(_,_,_,milk,_), _, _],
select( [ h(red,englishman,_,_,_), h(_,swede,dog,_,_),
h(_,dane,_,tea,_), h(_,german,_,_,prince) ], HS),
select( [ h(_,_,birds,_,pallmall), h(yellow,_,_,_,dunhill),
h(_,_,_,beer,bluemaster) ], HS),
left_of( h(green,_,_,coffee,_), h(white,_,_,_,_), HS),
next_to( h(_,_,_,_,dunhill), h(_,_,horse,_,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,cats, _,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,_,water,_), HS),
next_to( h(_,norwegian,_,_,_), h(blue,_,_,_,_), HS),
member( h(_,Owns,zebra,_,_), HS).
:- ?- time(( zebra(Who, HS), maplist(writeln,HS), nl, write(Who), nl, nl, fail
; write('No more solutions.') )).

View file

@ -0,0 +1,30 @@
% attribute store is 'Name - Value' pairs with unique names
attrs( H, [N-V | R]) :- !, memberchk( N-X, H), X = V, (R = [], ! ; attrs( H, R)).
attrs( HS, AttrsL) :- maplist( attrs, HS, AttrsL).
in( HS, Attrs) :- in( member, HS, Attrs).
in( G, HS, Attrs) :- call( G, A, HS), attrs( A, Attrs).
left_of( [A,B], HS) :- append( _, [A,B | _], HS).
next_to( [A,B], HS) :- left_of( [A,B], HS) ; left_of( [B,A], HS).
zebra( Owner, Houses):-
Houses = [A,_,C,_,_], % 1
maplist( in(Houses), [ [ nation-englishman, color-red ] % 2
, [ nation-swede, owns -dog ] % 3
, [ nation-dane, drink-tea ] % 4
, [ drink -coffee, color-green ] % 6
, [ smoke -'Pall Mall', owns -birds ] % 7
, [ color -yellow, smoke-'Dunhill' ] % 8
, [ drink -beer, smoke-'Blue Master'] % 13
, [ nation-german, smoke-'Prince' ] % 14
] ),
in( left_of, Houses, [[color -green ], [color -white ]]), % 5
in( left_of, [C,A], [[drink -milk ], [nation-norwegian]]), % 9, 10
maplist( in( next_to, Houses),
[ [[smoke -'Blend' ], [owns -cats ]] % 11
, [[owns -horse ], [smoke-'Dunhill' ]] % 12
, [[nation-norwegian], [color-blue ]] % 15
, [[drink -water ], [smoke-'Blend' ]] % 16
] ),
in( Houses, [owns-zebra, nation-Owner]).

View file

@ -0,0 +1,12 @@
?- time(( zebra(Z,HS), (maplist(length,HS,_) -> maplist(sort,HS,S),
maplist(writeln,S),nl,writeln(Z)), false ; writeln('No More Solutions'))).
[color-yellow,drink-water, nation-norwegian, owns-cats, smoke-Dunhill ]
[color-blue, drink-tea, nation-dane, owns-horse, smoke-Blend ]
[color-red, drink-milk, nation-englishman,owns-birds, smoke-Pall Mall ]
[color-green, drink-coffee,nation-german, owns-zebra, smoke-Prince ]
[color-white, drink-beer, nation-swede, owns-dog, smoke-Blue Master]
german
No More Solutions
% 263,486 inferences, 0.047 CPU in 0.063 seconds (74% CPU, 5630007 Lips)

View file

@ -0,0 +1,45 @@
:- initialization(main).
zebra(X) :-
houses(Hs), member(h(_,X,zebra,_,_), Hs)
, findall(_, (member(H,Hs), write(H), nl), _), nl
, write('the one who keeps zebra: '), write(X), nl
.
houses(Hs) :-
Hs = [_,_,_,_,_] % 1
, H3 = h(_,_,_,milk,_), Hs = [_,_,H3,_,_] % 9
, H1 = h(_,nvg,_,_,_ ), Hs = [H1|_] % 10
, maplist( flip(member,Hs),
[ h(red,eng,_,_,_) % 2
, h(_,swe,dog,_,_) % 3
, h(_,dan,_,tea,_) % 4
, h(green,_,_,coffe,_) % 6
, h(_,_,birds,_,pm) % 7
, h(yellow,_,_,_,dh) % 8
, h(_,_,_,beer,bm) % 13
, h(_,ger,_,_,pri) % 14
])
, infix([ h(green,_,_,_,_)
, h(white,_,_,_,_) ], Hs) % 5
, maplist( flip(nextto,Hs),
[ [h(_,_,_,_,bl ), h(_,_,cats,_,_)] % 11
, [h(_,_,horse,_,_), h(_,_,_,_,dh )] % 12
, [h(_,nvg,_,_,_ ), h(blue,_,_,_,_)] % 15
, [h(_,_,_,water,_), h(_,_,_,_,bl )] % 16
])
.
flip(F,X,Y) :- call(F,Y,X).
infix(Xs,Ys) :- append(Xs,_,Zs) , append(_,Zs,Ys).
nextto(P,Xs) :- permutation(P,R), infix(R,Xs).
main :- findall(_, (zebra(_), nl), _), halt.

View file

@ -0,0 +1,54 @@
:- use_module(library(clpfd)).
zebra :-
Nation = [Englishman, Spaniard, Japanese, Ukrainian, Norwegian ],
Color = [Red, Green, White, Yellow, Blue ],
Smoke = [Oldgold, Kools, Chesterfield, Luckystrike, Parliament],
Pet = [Dog, Snails, Fox, Horse, Zebra ],
Drink = [Tea, Coffee, Milk, Orangejuice, Water ],
% house numbers 1 to 5
Nation ins 1..5,
Color ins 1..5,
Smoke ins 1..5,
Pet ins 1..5,
Drink ins 1..5,
% the values in each list are exclusive
all_different(Nation),
all_different(Color),
all_different(Smoke),
all_different(Pet),
all_different(Drink),
% actual constraints
Englishman #= Red,
Spaniard #= Dog,
Green #= Coffee,
Ukrainian #= Tea,
Green #= White + 1,
Oldgold #= Snails,
Yellow #= Kools,
Milk #= 3,
Norwegian #= 1,
(Chesterfield #= Fox - 1 #\/ Chesterfield #= Fox + 1),
(Kools #= Horse - 1 #\/ Kools #= Horse + 1),
Luckystrike #= Orangejuice,
Japanese #= Parliament,
(Norwegian #= Blue - 1 #\/ Norwegian #= Blue + 1),
% get solution
flatten([Nation, Color, Smoke, Pet, Drink], List), label(List),
% print the answers
sort([Englishman-englishman, Spaniard-spaniard, Japanese-japanese, Ukrainian-ukrainian, Norwegian-norwegian], NationNames),
sort([Red-red, Green-green, White-white, Yellow-yellow, Blue-blue], ColorNames),
sort([Oldgold-oldgold, Kools-kools, Chesterfield-chesterfield, Luckystrike-luckystrike, Parliament-parliament], SmokeNames),
sort([Dog-dog, Snails-snails, Fox-fox, Horse-horse, Zebra-zebra], PetNames),
sort([Tea-tea, Coffee-coffee, Milk-milk, Orangejuice-orangejuice, Water-water], DrinkNames),
Fmt = '~w~16|~w~32|~w~48|~w~64|~w~n',
format(Fmt, NationNames),
format(Fmt, ColorNames),
format(Fmt, SmokeNames),
format(Fmt, PetNames),
format(Fmt, DrinkNames).