Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. outputFile('./neighbours_solved.txt').
  2. inputFile('./neighbours_unsolved.txt').
  3.  
  4. :- use_module(library(clpfd)).
  5.  
  6. solve(Size, Grid):-
  7. constraints(Size,Grid),
  8. removeNeighbours(Grid,Solution),
  9. transpose(Grid,Columns),
  10. removeNeighbourLine(Grid,VRows),
  11. maplist(neighCheck, VRows),
  12. removeNeighbourLine(Columns,VColumns),
  13. maplist(neighCheck, VColumns),
  14. maplist(all_distinct,Solution),
  15. transpose(Solution,SolutionCol),
  16. maplist(all_distinct,SolutionCol),
  17. maplist(label,Solution).
  18.  
  19. doSolve(neighbours(size(Size),grid(Problem)),neighbours(size(Size),grid(Solution))):-
  20. solve(Size, Problem),
  21. removeNeighbours(Problem, Solution).
  22.  
  23. one_apartx(X, Y) :- abs(X-Y) #= 1, #\=(X,Y).
  24. one_aparto(X, Y) :- abs(X-Y) #\= 1, #\=(X,Y).
  25.  
  26. neighCheck([_]).
  27. neighCheck([A,C,B|T]):-
  28. C = x,
  29. one_apartx(A,B),
  30. neighCheck([B|T]).
  31. neighCheck([A,C,B|T]):-
  32. C = o,
  33. one_aparto(A,B),
  34. neighCheck([B|T]).
  35.  
  36.  
  37. removeNeighbours([P],[S]):- removeNeighbourLine(P,S).
  38. removeNeighbours([P,_|PT],[S|ST]):- removeNeighbourLine(P,S), removeNeighbours(PT,ST).
  39.  
  40. removeNeighbourLine([P],[P]).
  41. removeNeighbourLine([P,_|PT],[P|ST]):- removeNeighbourLine(PT,ST).
  42.  
  43. replaceNeighbourLines(L,R):-
  44. replace('x',1,L,R1),
  45. replace('o',2,R1,R).
  46.  
  47. replaceNeighbours(Ls, Rs) :-
  48. maplist(replaceNeighbourLines, Ls, Rs).
  49.  
  50. removeNeighboursRows(Ls,Rs):-
  51. maplist(removeNeighbourLine, Ls, Rs).
  52.  
  53.  
  54. valid_numbAndNeigh(Size,[H|[]]):- H in 1..Size.
  55. valid_numbAndNeigh(Size,[H,N|T]):-
  56. H in 1..Size,
  57. N = x,
  58. valid_numbAndNeigh(Size,T).
  59.  
  60. valid_numbAndNeigh(Size,[H,N|T]):-
  61. H in 1..Size,
  62. N = o,
  63. valid_numbAndNeigh(Size,T).
  64.  
  65.  
  66. valid_neighbourLines([H|[]]):- H = x.
  67. valid_neighbourLines([H|[]]):- H = o.
  68. valid_neighbourLines([H,N|T]):-
  69. H = x,
  70. N = o,
  71. valid_neighbourLines(T).
  72.  
  73. valid_neighbourLines([H,N|T]):-
  74. H = o,
  75. N = o,
  76. valid_neighbourLines(T).
  77.  
  78. constraints(Size,[H|[]]):- valid_numbAndNeigh(Size,H).
  79. constraints(Size, [H,N|T]):-
  80. valid_numbAndNeigh(Size,H),
  81. valid_neighbourLines(N),
  82. constraints(Size,T).
  83.  
  84.  
  85.  
  86. /********************* writing the result */
  87. writeFullOutput(neighbours(size(N),grid(Grid))):-
  88. write('size '), write(N), write('x'), write(N), nl, writeGrid(Grid).
  89.  
  90. writeGrid([]).
  91. writeGrid([E|R]):- writeGridLine(E), writeGrid(R).
  92.  
  93. writeGridLine([]):- nl.
  94. writeGridLine([E|R]):- E='_', !, write(E), write(' '), writeGridLine(R).
  95. writeGridLine([E|R]):- write(E), write(' '), writeGridLine(R).
  96.  
  97. /********************** reading the input */
  98. readProblem(neighbours(size(N),grid(Grid))):-
  99. findKW(size), readInt(N), readInt(M), M=N, GridLength is N*2-1, length(Grid,GridLength),
  100. readGridLines(GridLength,Grid).
  101.  
  102. findKW(KW):- string_codes(KW,[H|T]), peek_code(H), readKW([H|T]), !.
  103. findKW(_):- peek_code(-1), !, fail.
  104. findKW(KW):- get_code(_), findKW(KW).
  105.  
  106. readKW([]):- get_code(_).
  107. readKW([H|T]):- get_code(H), readKW(T).
  108.  
  109. readGridLines(N,[A]):- length(A,N), readGridLine(A).
  110. readGridLines(N,[A,B|T]):- length(A,N), readGridLine(A), length(B,N), readNeighborLine(B), readGridLines(N,T).
  111.  
  112. readGridLine([N]):- readInt(I), makeHint(I,N).
  113. readGridLine([N,X|T]):- readInt(I), makeHint(I,N), get_code(M), translate(M,X), !, readGridLine(T).
  114.  
  115. readNeighborLine([X]):- get_code(M), translate(M,X), !.
  116. readNeighborLine([X,o|T]):- get_code(M), translate(M,X), get_code(_), get_code(_), get_code(_), !, readNeighborLine(T).
  117.  
  118. makeHint(X,X):- X>0.
  119. makeHint(0,_).
  120.  
  121. translate(-1,'ERROR: EOF').
  122. translate(120,'x').
  123. translate(32,'o').
  124. translate(X,X).
  125. translate(X,E):- whitespace(X), get_code(Y), translate(Y,E).
  126. translate(X,E):- string_codes(E,[X]).
  127.  
  128. whitespace(10). whitespace(12). whitespace(32).
  129.  
  130. readInt(N):- get_code(M), handleCode(M,N).
  131.  
  132. handleCode(M,N):- is_number_code(M,N1), !, continueInt(N1,N).
  133. handleCode(-1,_):- !, fail. /* EOF */
  134. handleCode(_,N):- readInt(N).
  135.  
  136. continueInt(O,N):- get_code(M), is_number_code(M,M1), !, H is 10*O+M1, continueInt(H,N).
  137. continueInt(N,N).
  138.  
  139. is_number_code(N, N1):- N>=48, N<58, N1 is N-48.
  140. is_number_code(95,0).
  141.  
  142. /*********************** global control: starting the algorithm and the reading */
  143. run:- inputFile(IF), see(IF), outputFile(F), tell(F), findKW(puzzles), readInt(N), write('puzzles '), write(N), nl, solveProblems(N), told, seen, !.
  144. run:- told, seen. /* close the files */
  145.  
  146. solveProblems(0).
  147. solveProblems(N):- N>0, readProblem(P), doSolve(P, S), writeFullOutput(S), !, N1 is N-1, solveProblems(N1).
  148.  
  149. :- nl,nl,write(' try running "?- run."'), nl,nl,nl.
  150.  
  151. :- run.
  152. :- halt.
  153.  
  154. /*
  155. mintest
  156.  
  157. [[_,x,_,x,_,o,_],[o,o,o,o,o,o,o],[_,o,_,x,_,x,_],[x,o,x,o,x,o,x],[_,x,_,x,_,o,_],[o,o,o,o,o,o,o],[4,o,_,x,_,x,3]]
  158. [[3,x,2,x,1,o,4],[o,o,o,o,o,o,o],[1,o,4,x,3,x,2],[x,o,x,o,x,o,x],[2,x,3,x,4,o,1],[o,o,o,o,o,o,o],[4,o,1,x,2,x,3]]
  159.  
  160. [[_,x,_,x,_,o,_],[_,o,_,x,_,x,_],[_,x,_,x,_,o,_],[4,o,_,x,_,x,3]]
  161.  
  162. [[3,1,2,1,1,2,4],[2,2,2,2,2,2,2],[1,2,4,1,3,1,2],[1,2,1,2,1,2,1],[2,1,3,1,4,2,1],[2,2,2,2,2,2,2],[4,2,1,1,2,1,_]]
  163. [[_,1,_,1,_,2,_],[2,2,2,2,2,2,2],[_,2,_,1,_,1,_],[1,2,1,2,1,2,1],[_,1,_,1,_,2,_],[2,2,2,2,2,2,2],[4,2,_,1,_,1,3]]
  164. [[_,x,_,o,_,x,_,o,_],[x,o,x,o,x,o,o,o,x],[_,x,_,o,_,o,_,o,_],[o,o,o,o,o,o,o,o,o],[1,o,5,o,_,x,_,o,_],[o,o,o,o,x,o,x,o,x],[_,x,_,x,_,o,_,o,_],[x,o,x,o,x,o,o,o,o],[_,x,_,o,_,x,_,x,_]]
  165.  
  166. 7x7 hard
  167. [[_,o,_,x,_,o,_,o,_,o,_,x,_],[x,o,o,o,o,o,x,o,o,o,o,o,o],[_,o,_,o,_,x,_,o,_,o,4,o,_],[o,o,x,o,o,o,x,o,o,o,o,o,o],[_,x,_,x,_,o,_,o,_,x,_,o,_],
  168. [x,o,o,o,o,o,o,o,o,o,o,o,x],[_,o,_,x,_,o,_,o,1,o,7,o,_],[o,o,o,o,o,o,o,o,o,o,o,o,o],[_,x,_,o,_,o,_,x,_,x,_,o,_],[o,o,o,o,o,o,x,o,o,o,o,o,o],
  169. [_,x,_,o,_,x,_,o,_,x,_,o,_],[o,o,o,o,o,o,x,o,o,o,o,o,o],[_,x,_,x,_,o,_,x,_,o,_,x,_]]
  170.  
  171. statistics(walltime, [TimeSinceStart | [TimeSinceLastCall]]),
  172. tester(5,[[_,x,_,o,_,x,_,o,_],[x,o,x,o,x,o,o,o,x],[_,x,_,o,_,o,_,o,_],[o,o,o,o,o,o,o,o,o],[1,o,5,o,_,x,_,o,_],[o,o,o,o,x,o,x,o,x],[_,x,_,x,_,o,_,o,_],[x,o,x,o,x,o,o,o,o],[_,x,_,o,_,x,_,x,_]]),
  173. statistics(walltime, [NewTimeSinceStart | [ExecutionTime]]),
  174. write('Execution took '), write(ExecutionTime), write(' ms.'), nl.
  175. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement