Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. 4%
  2.  
  3. stree(Graph,Tree):-member(Edge,Graph),
  4. spread([Edge],Tree,Graph).
  5. spread(Tree1,Tree,Graph):-addedge(Tree1,Tree2,Graph),spread(Tree2,Tree,Graph).
  6. spread(Tree,Tree,Graph):-not(addedge(Tree,_,Graph)).
  7. addedge(Tree,[A-B|Tree],Graph):-
  8. adjacent(A,B,Graph),node(A,Tree),not(node(B,Tree)).
  9. adjacent(Node1,Node2,Graph):-member(Node1-Node2,Graph);
  10. member(Node2-Node1,Graph).
  11. node(Node,Graph):-adjacent(Node,_,Graph).
  12.  
  13.  
  14. 5%
  15.  
  16.  
  17. path(From, To) :-
  18. traverse(From),
  19. edge([To|ReversedPath], Dist)->
  20. reverse([To|ReversedPath], Path),
  21. Distance is Dist,
  22. writef('Path = %w and the distance is %w = %w\n',
  23. [Path, Dist, Distance]);
  24. writef('There is no path that connects %w with %w\n', [From, To]).
  25.  
  26.  
  27. traverse(From) :-
  28. retractall(edge(_,_)),
  29. traverse(From,[],0).
  30.  
  31. traverse(From, Path, Dist) :-
  32. path(From, T, D),
  33. not(memberchk(T, Path)),
  34. shorterPath([T,From|Path], Dist+D),
  35. traverse(T,[From|Path],Dist+D).
  36.  
  37. traverse(_).
  38.  
  39. path(From,To,Dist) :- neighbour(From,To,Dist).
  40.  
  41. shorterPath([H|Path], Dist) :-
  42. edge([H|_], D), !, Dist < D,
  43. retract(edge([H|_],_)),
  44. assert(edge([H|Path], Dist)).
  45.  
  46. shorterPath(Path, Dist) :-
  47. assert(edge(Path,Dist)).
  48.  
  49.  
  50. checkCell((X1,Y1),(X2,Y2)) :- checkCell1(X1), checkCell1(Y1), checkCell1(X2), checkCell1(Y2).
  51. checkCell1(X) :- 1 =< X, X =< 8 .
  52.  
  53. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1+2, Y2 is Y1+1, checkCell((X1,Y1),(X2,Y2)).
  54.  
  55. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1+1, Y2 is Y1+2, checkCell((X1,Y1),(X2,Y2)).
  56.  
  57. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1+2, Y2 is Y1-1, checkCell((X1,Y1),(X2,Y2)).
  58.  
  59. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1+1, Y2 is Y1-2, checkCell((X1,Y1),(X2,Y2)).
  60.  
  61. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1-2, Y2 is Y1+1, checkCell((X1,Y1),(X2,Y2)).
  62.  
  63. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1-1, Y2 is Y1+2, checkCell((X1,Y1),(X2,Y2)).
  64.  
  65. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1-2, Y2 is Y1-1, checkCell((X1,Y1),(X2,Y2)).
  66.  
  67. neighbour((X1,Y1),(X2,Y2), 1) :- X2 is X1-1, Y2 is Y1-2, checkCell((X1,Y1),(X2,Y2)).
  68.  
  69. %6
  70.  
  71. :- module(lifec, [play/0]).
  72.  
  73. play :-
  74. grid(G),
  75. lifec(G).
  76.  
  77.  
  78. grid([
  79. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  80. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  81. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  82. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  83. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  84. [0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
  85. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  86. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  87. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  88. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  89. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  90. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  91. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  92. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  93. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  94. ]
  95. ).
  96.  
  97. lifec(Grid) :-
  98. make_ints(Grid, Ints, Size),
  99. lifei(Ints, Size).
  100.  
  101. lifei(Ints, Size) :-
  102. dumpgen(Ints, Size),
  103. onegen(Ints, Size, NewInts),
  104. get_single_char(_),
  105. !, lifei(NewInts, Size).
  106.  
  107. dumpgen(Ints, Size) :-
  108. forall(member(I, Ints),
  109. ( for_next(1, Size, _, show_bit(I)), nl) ).
  110.  
  111. onegen(Matrix, Size, NewMatrix) :-
  112. findall(NewBits,
  113. (three_rows(Matrix, Size, Rows),
  114. rowstate(Rows, 0, Size, 0, NewBits)), NewMatrix).
  115.  
  116. three_rows(Matrix, Size, Rows) :-
  117. nth1(I, Matrix, Row),
  118. ( I > 1 -> U is I - 1, nth1(U, Matrix, Up) ; Up = 0 ),
  119. ( I < Size -> D is I + 1, nth1(D, Matrix, Down) ; Down = 0 ),
  120. % padding: add 0 bit to rightmost position
  121. maplist(lshift, [Up, Row, Down], Rows).
  122.  
  123. :- dynamic evopatt/2.
  124.  
  125. rowstate([_, _, _], Size, Size, NewBits, NewBits) :- !.
  126. rowstate([U, R, D], I, Size, Accum, Result) :-
  127. Key is (U /\ 7) \/ ((R /\ 7) << 3) \/ ((D /\ 7) << 6),
  128. evopatt(Key, Bit),
  129. Accum1 is Accum \/ (Bit << I),
  130. maplist(rshift, [U,R,D], P),
  131. J is I + 1,
  132. rowstate(P, J, Size, Accum1, Result).
  133.  
  134.  
  135. make_ints(Grid, Ints, Size) :-
  136. length(Grid, Size),
  137. maplist(set_bits(0, 0), Grid, Ints),
  138. % precompute evolution patterns
  139. retractall(evopatt(_, _)),
  140. for_next(0, 511, _, add_evopatt).
  141.  
  142. add_evopatt(N) :-
  143. maplist(take_bit(N), [0,1,2], U),
  144. maplist(take_bit(N), [3,4,5], V),
  145. maplist(take_bit(N), [6,7,8], Z),
  146. rule(U, V, Z, Bit),
  147. assert(evopatt(N, Bit)).
  148.  
  149.  
  150. rule([A,B,C],[D,0,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 3.
  151. rule([_,_,_],[_,0,_],[_,_,_],0).
  152. rule([A,B,C],[D,1,F],[G,H,I],0) :- A+B+C+D+F+G+H+I < 2.
  153. rule([A,B,C],[D,1,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 2.
  154. rule([A,B,C],[D,1,F],[G,H,I],1) :- A+B+C+D+F+G+H+I =:= 3.
  155. rule([A,B,C],[D,1,F],[G,H,I],0) :- A+B+C+D+F+G+H+I > 3.
  156.  
  157.  
  158. :- meta_predicate for_next(+,+,-,1).
  159.  
  160. for_next(From, To, N, Pred) :-
  161. forall(between(From, To, N), call(Pred, N)).
  162.  
  163. lshift(X, Y) :- Y is X << 1.
  164. rshift(X, Y) :- Y is X >> 1.
  165.  
  166. show_bit(I, P) :-
  167. take_bit(I, P - 1, 1) -> put(0'*) ; put(0' ).
  168.  
  169. take_bit(N, Pos, Bit) :-
  170. Bit is (N >> Pos) /\ 1.
  171.  
  172. set_bits(_Index, Accum, [], Accum).
  173. set_bits(Index, Accum, [ZeroOne|Rest], Number) :-
  174. Accum1 is Accum \/ (ZeroOne << Index),
  175. Index1 is Index + 1,
  176. set_bits(Index1, Accum1, Rest, Number).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement