Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- use_module(library(clpfd)).
- money(Root) :-
- Vars = [A,B,C,D,E,F,G,H],
- Vars ins 0..9,
- Root = sol(a:A,b:B,c:C,d:D,e:M,o:O,r:R,y:Y),
- all_different(Vars),
- M #\= 0, S #\= 0,
- S*1000 + E*100 + N*10 + D +
- M*1000 + O*100 + R*10 + E #=
- M*10000 + O*1000 + N*100 + E*10 + Y,
- labeling([leftmost],Vars).
- przyk15(Sol) :-
- X in 2..8,
- Y in 7..9,
- Sol = [X,Y],
- X * Y #< 29,
- 2 * X + Y #> 13,
- labeling([leftmost],Sol).
- password(Digits) :-
- Digits = [D1,D2,D3,D4,D5,D6,D7,D8,D9],
- Digits ins 1..9,
- all_different(Digits),
- D4-D6 #= D7,
- D1*D2*D3 #= D8+D9,
- D2+D3+D6 #< D8,
- D9 #< D8,
- positionNotEqualToDigit(Digits,1),
- labeling([ff], Digits).
- positionNotEqualToDigit([],_) :- !.
- positionNotEqualToDigit([D|Rest],P) :-
- D #\= P,
- P1 is P + 1,
- positionNotEqualToDigit(Rest,P1).
- grocery(Vars) :-
- Vars = [A,B,C,D], S is 711,
- Vars ins 1..S,
- A * B * C * D #= S * 100 * 100 * 100,
- A + B + C + D #= S,
- B #=< C,
- C #=< D,
- labeling([ff],Vars).
- map_color(Result):-
- Data =
- [[austria, [italy,switzerland,germany]],
- [belgium, [france,netherlands,germany,
- luxemburg]],
- [france, [spain,luxemburg,italy]],
- [germany, [austria,france,luxemburg,
- netherlands]],
- [italy, []],
- [luxemburg, []],
- [netherlands, []],
- [portugal, []],
- [spain, [portugal]],
- [switzerland, [italy,france,germany,
- austria]]],
- maplist(country,Data,Countries),
- length(Countries,NbCountries),
- length(CountryColors,NbCountries),
- %NbColors in 1..NbCountries,
- %labeling([ff],[NbColors]),
- CountryColors ins 1..NbCountries,
- maplist(color(Countries,CountryColors),Data),
- labeling([ff],CountryColors),
- maplist(result,Countries,CountryColors,Result).
- country([Country,_],Country).
- color(Cnts,CntCols,[Cnt,Neighs]) :-
- country_color(Cnts,CntCols,Cnt,CntCol),
- maplist(color1(Cnts,CntCols,CntCol),Neighs).
- result(Cnt,CntCol,Cnt:CntCol).
- color1(Cnts,CntCols,CntCol,Neigh) :-
- country_color(Cnts,CntCols,Neigh,NeighCol),
- CntCol #\= NeighCol.
- country_color([Cnt|_],[Col|_],Cnt,Col).
- country_color([_|Cnts],[_|Cols],Cnt,Col) :-
- country_color(Cnts,Cols,Cnt,Col).
- change(Coins,Amount,Changes) :-
- maplist(split_pair,Coins,Denoms,Avails),
- maplist(avail_constr,Avails,Changes),
- scalar_product(Denoms,Changes,#=,Amount),
- sum(Changes,#=,CoinsNo),
- labeling([min(CoinsNo)],Changes).
- split_pair(D:A,D,A).
- avail_constr(Avail,Change) :- Change in 0..Avail.
- solve(N) :-
- latin_square(N, Square),
- maplist(writeln, Square).
- latin_square(N, Square) :-
- length(Square, N),
- maplist(same_length(Square),Square),
- append(Square,AllVars),
- AllVars ins 1..N,
- maplist(all_different, Square),
- transpose(Square, TSquare),
- maplist(all_different, TSquare),
- labeling([ff], AllVars).
- n_queens(N, Qs) :-
- length(Qs, N),
- Qs ins 1..N,
- safe_queens(Qs),
- labeling([ff],Qs).
- safe_queens([]).
- safe_queens([Q|Qs]) :-
- safe_queens(Qs, Q, 1),
- safe_queens(Qs).
- safe_queens([], _, _).
- safe_queens([Q|Qs], Q0, D0) :-
- Q0 #\= Q,
- abs(Q0 - Q) #\= D0,
- D1 #= D0 + 1,
- safe_queens(Qs, Q0, D1).
- make_square(0,_,[]) :- !.
- make_square(I,N,[Row|Rest]) :-
- length(Row, N),
- I1 is I - 1,
- make_square(I1, N, Rest).
- magic_square(N, Square) :-
- make_square(N,N,Square),
- append(Square,AllVars),
- AllVars ins 1..N,
- maplist(all_different, Square),
- transpose(Square, TSquare),
- maplist(all_different, TSquare),
- list_diag1(Square, D1),
- list_diag2(Square, D2),
- all_different(D1),
- all_different(D2),
- labeling([ff], AllVars),
- maplist(writeln, Square).
- list_diag1([], []).
- list_diag1([[E|_]|Ess], [E|Ds]) :-
- maplist(list_tail, Ess, Ess0),
- list_diag1(Ess0, Ds).
- list_tail([_|Es], Es).
- list_diag2(Ess,Ds) :-
- maplist(reverse, Ess, Fss),
- list_diag1(Fss, Ds).
- graph_coloring(Graph, Coloring) :-
- length(Graph,N),
- length(Coloring,N),
- length(Tints,N),
- Cn in 1..N,
- labeling([ff],[Cn]),
- Coloring ins 1..Cn,
- constr(Graph,Coloring,Tints),
- all_different(Tints),
- labeling([ff],Coloring).
- constr(Graph,Coloring,Tints) :-
- maplist(vert_sum(Coloring), Graph, Tints).
- vert_sum(VertexColoring,VertNeighInds, VertSum) :-
- maplist(ind_to_vert(VertexColoring),VertNeighInds,VertNeighs),
- sum(VertNeighs, #=, VertSum).
- ind_to_vert(VertexColoring,VertI,Vert) :-
- nth1(VertI,VertexColoring,Vert).
- visit(sol(fs:FS, hs:HS, j:J, m:M, t:T)) :-
- Vars = [FS, HS, J, M, T],
- Vars ins 0..1,
- HS #<== FS,
- M #\/ J,
- FS #\ T,
- T #<==> J,
- M #==> (J #/\ HS),
- labeling([ff],Vars).
- equations(sol(a:A, b:B, c:C, d:D, e:E, f:F, g:G, h:H)) :-
- Vars = [A, B, C, D, E, F, G, H],
- Vars ins 1..10,
- all_different(Vars),
- A + E #= G,
- C + D #= 10,
- E + F #= 8,
- A + C #= 6,
- F #\= 6 #==> H #> F,
- E #\= 1 #==> H #> B,
- G #\= 8 #==> F #> B,
- B #\= 5 #==> G #\= 5,
- E #\= 3 #==> C #\= 4,
- labeling([ff],Vars).
- horses1(sol(p:P, n:N, w:W, h:H, s:S)) :-
- Vars = [P,N,W,H,S],
- Vars ins 1..5,
- P #< N,
- P #> W,
- H #\= S #==> P #= W,
- W #> N #<==> (P - S #= S - W),
- labeling([ff], Vars).
- horses(sol(p:P, n:N, w:W, h:H, s:S)) :-
- Vars = [P, N, W, H, S],
- Vars ins 1..5,
- P #< N, P #> W,
- (W #= P) #<==> (H #\= S),
- (P - S #= S - W) #<==> W #> N,
- labeling([ff],Vars).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement