Advertisement
jalih

Sudoku solver 8th

Feb 9th, 2023 (edited)
84
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | Source Code | 0 0
  1. \
  2. \ Simple backtracking Sudoku solver for the 8th programming language
  3. \
  4.  
  5. \ Sub-board window for the given board index
  6. [ 00, 00, 00, 01, 01, 01, 02, 02, 02,
  7. 00, 00, 00, 01, 01, 01, 02, 02, 02,
  8. 00, 00, 00, 01, 01, 01, 02, 02, 02,
  9. 03, 03, 03, 04, 04, 04, 05, 05, 05,
  10. 03, 03, 03, 04, 04, 04, 05, 05, 05,
  11. 03, 03, 03, 04, 04, 04, 05, 05, 05,
  12. 06, 06, 06, 07, 07, 07, 08, 08, 08,
  13. 06, 06, 06, 07, 07, 07, 08, 08, 08,
  14. 06, 06, 06, 07, 07, 07, 08, 08, 08
  15. ] ( swap a:_@ ) curry: window? \ n -- n
  16.  
  17. \ Sub-board indices for the given window
  18. [
  19. [00,01,02,09,10,11,18,19,20],
  20. [03,04,05,12,13,14,21,22,23],
  21. [06,07,08,15,16,17,24,25,26],
  22. [27,28,29,36,37,38,45,46,47],
  23. [30,31,32,39,40,41,48,49,50],
  24. [33,34,35,42,43,44,51,52,53],
  25. [54,55,56,63,64,65,72,73,74],
  26. [57,58,59,66,67,68,75,76,77],
  27. [60,61,62,69,70,71,78,79,80]
  28. ] ( swap a:_@ a:_@ ) curry: sub? \ a n -- a
  29.  
  30. [
  31. [0,1,2,3,4,5,6,7,8],
  32. [9,10,11,12,13,14,15,16,17],
  33. [18,19,20,21,22,23,24,25,26],
  34. [27,28,29,30,31,32,33,34,35],
  35. [36,37,38,39,40,41,42,43,44],
  36. [45,46,47,48,49,50,51,52,53],
  37. [54,55,56,57,58,59,60,61,62],
  38. [63,64,65,66,67,68,69,70,71],
  39. [72,73,74,75,76,77,78,79,80]
  40. ] ( swap a:_@ a:_@ ) curry: row? \ a n -- a
  41.  
  42. [
  43. [0,9,18,27,36,45,54,63],
  44. [1,10,19,28,37,46,55,64,73],
  45. [2,11,20,29,38,47,56,65,74],
  46. [3,12,21,30,39,48,57,66,75],
  47. [4,13,22,31,40,49,58,67,76],
  48. [5,14,23,32,41,50,59,68,77],
  49. [6,15,24,33,42,51,60,69,78],
  50. [7,16,25,34,43,52,61,70,79],
  51. [8,17,26,35,44,53,62,71,80]
  52. ] ( swap a:_@ a:_@ ) curry: col? \ a n -- a
  53.  
  54. : trailing-zero-bits \ n -- n
  55. 32 >r
  56. dup n:neg n:band
  57. dup if -1 n:r+ then
  58. dup x0000ffff n:band if -16 n:r+ then
  59. dup x00ff00ff n:band if -8 n:r+ then
  60. dup x0f0f0f0f n:band if -4 n:r+ then
  61. dup x33333333 n:band if -2 n:r+ then
  62. x55555555 n:band if -1 n:r+ then
  63. r> ;
  64.  
  65. \ Bit number presentations
  66. a:new 0 a:push ( 1 swap n:shl a:push ) 0 8 loop
  67. ( swap a:_@ ) curry: posbit?
  68.  
  69. : search \ n -- n n | n null
  70. dup trailing-zero-bits dup 8 n:> if
  71. drop null
  72. then ;
  73.  
  74. : b-xor \ n n -- n
  75. n:bxor 511 n:band ;
  76.  
  77. : b-not \ n n -- n
  78. n:bnot 511 n:band ;
  79.  
  80. : b-any \ a -- n
  81. ' n:bor 0 a:reduce ;
  82.  
  83. a:new 0 args "Give Sudoku text file as param" thrownull
  84. f:slurp "Cannot read file" thrownull >s "\n" "" s:replace "" s:/
  85. ' >n a:map ( posbit? "Bad data" thrownull a:push ) a:each! drop constant board
  86.  
  87. : display-board
  88. board ( search nip -1 ?: n:1+ ) a:map
  89. "+-----+-----+-----+\n"
  90. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  91. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  92. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  93. "+-----+-----+-----+\n" s:+
  94. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  95. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  96. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  97. "+-----+-----+-----+\n" s:+
  98. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  99. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  100. "|%d %d %d|%d %d %d|%d %d %d|\n" s:+
  101. "+-----+-----+-----+\n" s:+
  102. s:strfmt . ;
  103.  
  104. \ Store move history
  105. a:new constant history
  106.  
  107. \ Possible numbers for a cell
  108. : candidates? \ n -- n
  109. dup dup 9 n:/ n:int swap 9 n:mod \ row col
  110. board swap col? b-any
  111. board rot row? b-any
  112. n:bor
  113. board rot window? sub? b-any
  114. n:bor
  115. b-not ;
  116.  
  117. \ If found: -- n T
  118. \ If not found: -- F
  119. : find-free-cell
  120. false board
  121. ( !if
  122. nip true break
  123. else
  124. drop
  125. then ) a:each drop ;
  126.  
  127. : validate \ -- T
  128. true
  129. board
  130. ( dup -rot a:@ swap 2 pick 0 a:! 2 pick candidates? 2 pick n:= if
  131. -rot a:!
  132. else
  133. 3drop
  134. false swap
  135. break
  136. then ) 0 80 loop drop ;
  137.  
  138. : solve \ -- T
  139. repeat
  140. find-free-cell if
  141. dup candidates?
  142. repeat
  143. search null? if
  144. drop board -rot a:! drop
  145. history a:len !if
  146. drop false ;;
  147. then
  148. a:pop nip
  149. a:open
  150. else
  151. n:1+ posbit? dup
  152. board 4 pick rot a:! drop
  153. b-xor
  154. 2 a:close
  155. history swap a:push drop
  156. break
  157. then
  158. again
  159. else
  160. validate break
  161. then
  162. again ;
  163.  
  164. : app:main
  165. "Sudoku puzzle:\n" .
  166. display-board cr
  167. solve if
  168. "Sudoku solved:\n" .
  169. display-board
  170. else
  171. "No solution!\n" .
  172. then ;
  173.  
Tags: Sudoku 8th
Advertisement
Comments
  • jalih
    1 year
    Comment was deleted
Add Comment
Please, Sign In to add comment
Advertisement