Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.50 KB | None | 0 0
  1. :- use_module(library(clpfd)).
  2.  
  3. money(Root) :-
  4.    Vars = [A,B,C,D,E,F,G,H],
  5.    Vars ins 0..9,
  6.    Root = sol(a:A,b:B,c:C,d:D,e:M,o:O,r:R,y:Y),
  7.    all_different(Vars),
  8.    M #\= 0, S #\= 0,
  9.    S*1000 + E*100 + N*10 + D +
  10.    M*1000 + O*100 + R*10 + E #=
  11.    M*10000 + O*1000 + N*100 + E*10 + Y,
  12.    labeling([leftmost],Vars).
  13.  
  14. przyk15(Sol) :-
  15.     X in 2..8,
  16.     Y in 7..9,
  17.     Sol = [X,Y],
  18.     X * Y #< 29,
  19.     2 * X + Y #> 13,
  20.     labeling([leftmost],Sol).
  21.  
  22. password(Digits) :-
  23.     Digits = [D1,D2,D3,D4,D5,D6,D7,D8,D9],
  24.     Digits ins 1..9,
  25.     all_different(Digits),
  26.     D4-D6 #= D7,
  27.     D1*D2*D3 #= D8+D9,
  28.     D2+D3+D6 #< D8,
  29.     D9 #< D8,
  30.     positionNotEqualToDigit(Digits,1),
  31.     labeling([ff], Digits).
  32.  
  33. positionNotEqualToDigit([],_) :- !.
  34. positionNotEqualToDigit([D|Rest],P) :-
  35.     D #\= P,
  36.     P1 is P + 1,
  37.     positionNotEqualToDigit(Rest,P1).
  38.  
  39.  
  40. grocery(Vars) :-
  41.     Vars = [A,B,C,D], S is 711,
  42.     Vars ins 1..S,
  43.     A * B * C * D #= S * 100 * 100 * 100,
  44.     A + B + C + D #= S,
  45.     B #=< C,
  46.     C #=< D,
  47.     labeling([ff],Vars).
  48.  
  49.  
  50. map_color(Result):-
  51.    Data =
  52.       [[austria, [italy,switzerland,germany]],
  53.        [belgium, [france,netherlands,germany,
  54.                   luxemburg]],
  55.        [france, [spain,luxemburg,italy]],
  56.        [germany, [austria,france,luxemburg,
  57.                   netherlands]],
  58.        [italy, []],
  59.        [luxemburg, []],
  60.        [netherlands, []],
  61.        [portugal, []],
  62.        [spain, [portugal]],
  63.        [switzerland, [italy,france,germany,
  64.                       austria]]],
  65.    maplist(country,Data,Countries),
  66.    length(Countries,NbCountries),
  67.    length(CountryColors,NbCountries),
  68.    %NbColors in 1..NbCountries,
  69.    %labeling([ff],[NbColors]),
  70.    CountryColors ins 1..NbCountries,
  71.    maplist(color(Countries,CountryColors),Data),
  72.    labeling([ff],CountryColors),
  73.    maplist(result,Countries,CountryColors,Result).
  74.  
  75.  
  76. country([Country,_],Country).
  77. color(Cnts,CntCols,[Cnt,Neighs]) :-
  78.    country_color(Cnts,CntCols,Cnt,CntCol),
  79.    maplist(color1(Cnts,CntCols,CntCol),Neighs).
  80. result(Cnt,CntCol,Cnt:CntCol).
  81.  
  82. color1(Cnts,CntCols,CntCol,Neigh) :-
  83.    country_color(Cnts,CntCols,Neigh,NeighCol),
  84.    CntCol #\= NeighCol.
  85. country_color([Cnt|_],[Col|_],Cnt,Col).
  86. country_color([_|Cnts],[_|Cols],Cnt,Col) :-
  87.    country_color(Cnts,Cols,Cnt,Col).
  88.  
  89.  
  90. change(Coins,Amount,Changes) :-
  91.    maplist(split_pair,Coins,Denoms,Avails),
  92.    maplist(avail_constr,Avails,Changes),
  93.    scalar_product(Denoms,Changes,#=,Amount),
  94.    sum(Changes,#=,CoinsNo),
  95.    labeling([min(CoinsNo)],Changes).
  96. split_pair(D:A,D,A).
  97. avail_constr(Avail,Change) :- Change in 0..Avail.
  98.  
  99. solve(N) :-
  100.     latin_square(N, Square),
  101.     maplist(writeln, Square).
  102.  
  103. latin_square(N, Square) :-
  104.     length(Square, N),
  105.     maplist(same_length(Square),Square),
  106.     append(Square,AllVars),
  107.     AllVars ins 1..N,
  108.     maplist(all_different, Square),
  109.     transpose(Square, TSquare),
  110.     maplist(all_different, TSquare),
  111.     labeling([ff], AllVars).
  112.  
  113. n_queens(N, Qs) :-
  114.     length(Qs, N),
  115.     Qs ins 1..N,
  116.     safe_queens(Qs),
  117.     labeling([ff],Qs).
  118.  
  119. safe_queens([]).
  120. safe_queens([Q|Qs]) :-
  121.     safe_queens(Qs, Q, 1),
  122.     safe_queens(Qs).
  123.  
  124. safe_queens([], _, _).
  125. safe_queens([Q|Qs], Q0, D0) :-
  126.     Q0 #\= Q,
  127.     abs(Q0 - Q) #\= D0,
  128.     D1 #= D0 + 1,
  129.     safe_queens(Qs, Q0, D1).
  130.  
  131.  
  132. make_square(0,_,[]) :- !.
  133. make_square(I,N,[Row|Rest]) :-
  134.     length(Row, N),
  135.     I1 is I - 1,
  136.     make_square(I1, N, Rest).
  137.  
  138.  
  139. magic_square(N, Square) :-
  140.     make_square(N,N,Square),
  141.     append(Square,AllVars),
  142.     AllVars ins 1..N,
  143.     maplist(all_different, Square),
  144.     transpose(Square, TSquare),
  145.     maplist(all_different, TSquare),
  146.     list_diag1(Square, D1),
  147.     list_diag2(Square, D2),
  148.     all_different(D1),
  149.     all_different(D2),
  150.     labeling([ff], AllVars),
  151.     maplist(writeln, Square).
  152.  
  153. list_diag1([], []).
  154. list_diag1([[E|_]|Ess], [E|Ds]) :-
  155.     maplist(list_tail, Ess, Ess0),
  156.     list_diag1(Ess0, Ds).
  157.  
  158. list_tail([_|Es], Es).
  159.  
  160. list_diag2(Ess,Ds) :-
  161.     maplist(reverse, Ess, Fss),
  162.     list_diag1(Fss, Ds).
  163.  
  164. graph_coloring(Graph, Coloring) :-
  165.     length(Graph,N),
  166.     length(Coloring,N),
  167.     length(Tints,N),   
  168.     Cn in 1..N,
  169.     labeling([ff],[Cn]),
  170.     Coloring ins 1..Cn,
  171.     constr(Graph,Coloring,Tints),
  172.     all_different(Tints),
  173.     labeling([ff],Coloring).   
  174.    
  175. constr(Graph,Coloring,Tints) :-
  176.     maplist(vert_sum(Coloring), Graph, Tints).
  177.  
  178. vert_sum(VertexColoring,VertNeighInds, VertSum) :-
  179.     maplist(ind_to_vert(VertexColoring),VertNeighInds,VertNeighs),
  180.     sum(VertNeighs, #=, VertSum).
  181.    
  182. ind_to_vert(VertexColoring,VertI,Vert) :-
  183.     nth1(VertI,VertexColoring,Vert).
  184.  
  185.  
  186. visit(sol(fs:FS, hs:HS, j:J, m:M, t:T)) :-
  187.    Vars = [FS, HS, J, M, T],
  188.    Vars ins 0..1,
  189.    HS #<== FS,
  190.    M #\/ J,
  191.    FS #\ T,
  192.    T #<==> J,
  193.    M #==> (J #/\ HS),
  194.    labeling([ff],Vars).
  195.  
  196.  
  197. equations(sol(a:A, b:B, c:C, d:D, e:E, f:F, g:G, h:H)) :-
  198.    Vars = [A, B, C, D, E, F, G, H],
  199.    Vars ins 1..10,
  200.    all_different(Vars),
  201.    A + E #= G,
  202.    C + D #= 10,
  203.    E + F #= 8,
  204.    A + C #= 6,
  205.    F #\= 6 #==> H #> F,
  206.    E #\= 1 #==> H #> B,
  207.    G #\= 8 #==> F #> B,
  208.    B #\= 5 #==> G #\= 5,
  209.    E #\= 3 #==> C #\= 4,
  210.    labeling([ff],Vars).
  211.  
  212. horses1(sol(p:P, n:N, w:W, h:H, s:S)) :-
  213.     Vars = [P,N,W,H,S],
  214.     Vars ins 1..5,
  215.     P #< N,
  216.     P #> W,
  217.     H #\= S #==> P #= W,
  218.     W #> N #<==> (P - S #= S - W),  
  219.     labeling([ff], Vars).
  220.  
  221. horses(sol(p:P, n:N, w:W, h:H, s:S)) :-
  222.    Vars = [P, N, W, H, S],
  223.    Vars ins 1..5,
  224.    P #< N, P #> W,
  225.    (W #= P) #<==> (H #\= S),
  226.    (P - S #= S - W) #<==> W #> N,
  227.    labeling([ff],Vars).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement