sudoku_solver_iterative.sf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/ruby
  2. # Author: Trizen
  3. # Date: 12 February 2024
  4. # https://github.com/trizen
  5. # Fast algorithm to solve a Sudoku puzzle (iterative solution).
  6. func is_valid(board, row, col, num) {
  7. # Check if the number is not present in the current row and column
  8. for i in ^9 {
  9. if ((board[row][i] == num) || (board[i][col] == num)) {
  10. return false
  11. }
  12. }
  13. # Check if the number is not present in the current 3x3 subgrid
  14. var (start_row, start_col) = (3*idiv(row, 3), 3*idiv(col, 3))
  15. for i in ^3, j in ^3 {
  16. if (board[start_row + i][start_col + j] == num) {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func find_empty_locations(board) {
  23. var locations = []
  24. # Find all empty positions (cells with 0)
  25. for i in ^9, j in ^9 {
  26. if (board[i][j] == 0) {
  27. locations << [i, j]
  28. }
  29. }
  30. return locations
  31. }
  32. func find_empty_location(board) {
  33. # Find an empty position (cell with 0)
  34. for i in ^9, j in ^9 {
  35. if (board[i][j] == 0) {
  36. return (i, j)
  37. }
  38. }
  39. return (nil, nil) # If the board is filled
  40. }
  41. func solve_sudoku_fallback(board) {
  42. var (row, col) = find_empty_location(board)
  43. if (!defined(row) && !defined(col)) {
  44. return true # Puzzle is solved
  45. }
  46. for num in (1..9) {
  47. if (is_valid(board, row, col, num)) {
  48. # Try placing the number
  49. board[row][col] = num
  50. # Recursively try to solve the rest of the puzzle
  51. if (__FUNC__(board)) {
  52. return true
  53. }
  54. # If placing the current number doesn't lead to a solution, backtrack
  55. board[row][col] = 0
  56. }
  57. }
  58. return false # No solution found
  59. }
  60. func solve_sudoku(board) {
  61. loop {
  62. var empty_locations = find_empty_locations(board) || break
  63. var found = false
  64. # Solve easy cases
  65. for i,j in empty_locations {
  66. var(count=0, value=0)
  67. for n in (1..9) {
  68. is_valid(board, i, j, n) || next
  69. break if (++count > 1)
  70. value = n
  71. }
  72. if (count == 1) {
  73. board[i][j] = value
  74. found ||= true
  75. }
  76. }
  77. next if found
  78. # Solve more complex cases
  79. var stats = []
  80. for i,j in empty_locations {
  81. stats[i][j] = (1..9 -> grep{|n| is_valid(board, i, j, n) })
  82. }
  83. var cols = []
  84. var rows = []
  85. var subgrid = []
  86. for i,j in empty_locations {
  87. stats[i][j].each {|v|
  88. cols[j][v] := 0 ++
  89. rows[i][v] := 0 ++
  90. subgrid[3*idiv(i,3)][3*idiv(j,3)][v] := 0 ++
  91. }
  92. }
  93. for i,j in empty_locations {
  94. stats[i][j].each {|v|
  95. if ((cols[j][v] == 1) ||
  96. (rows[i][v] == 1) ||
  97. (subgrid[3*idiv(i,3)][3*idiv(j,3)][v] == 1)
  98. ) {
  99. board[i][j] = v
  100. found ||= true
  101. }
  102. }
  103. }
  104. next if found
  105. # Give up and try brute-force
  106. solve_sudoku_fallback(board)
  107. return board
  108. }
  109. return board
  110. }
  111. # Example usage:
  112. # Define the Sudoku puzzle as a 9x9 list with 0 representing empty cells
  113. var sudoku_board = %n(
  114. 2 0 0 0 7 0 0 0 3
  115. 1 0 0 0 0 0 0 8 0
  116. 0 0 4 2 0 9 0 0 5
  117. 9 4 0 0 0 0 6 0 8
  118. 0 0 0 8 0 0 0 9 0
  119. 0 0 0 0 0 0 0 7 0
  120. 7 2 1 9 0 8 0 6 0
  121. 0 3 0 0 2 7 1 0 0
  122. 4 0 0 0 0 3 0 0 0
  123. ).slices(9)
  124. sudoku_board = %n(
  125. 0 0 0 8 0 1 0 0 0
  126. 0 0 0 0 0 0 0 4 3
  127. 5 0 0 0 0 0 0 0 0
  128. 0 0 0 0 7 0 8 0 0
  129. 0 0 0 0 0 0 1 0 0
  130. 0 2 0 0 3 0 0 0 0
  131. 6 0 0 0 0 0 0 7 5
  132. 0 0 3 4 0 0 0 0 0
  133. 0 0 0 2 0 0 6 0 0
  134. ).slices(9) if true
  135. sudoku_board = %n(
  136. 8 0 0 0 0 0 0 0 0
  137. 0 0 3 6 0 0 0 0 0
  138. 0 7 0 0 9 0 2 0 0
  139. 0 5 0 0 0 7 0 0 0
  140. 0 0 0 0 4 5 7 0 0
  141. 0 0 0 1 0 0 0 3 0
  142. 0 0 1 0 0 0 0 6 8
  143. 0 0 8 5 0 0 0 1 0
  144. 0 9 0 0 0 0 4 0 0
  145. ).slices(9) if false
  146. sudoku_board = %n(
  147. 0 0 1 0 6 0 0 5 9
  148. 0 0 0 0 0 3 0 2 0
  149. 0 6 0 0 8 0 0 0 0
  150. 4 0 0 0 0 0 5 0 0
  151. 0 2 0 0 0 0 0 0 0
  152. 0 7 0 2 0 0 4 8 0
  153. 8 0 0 0 0 0 9 0 5
  154. 7 0 0 6 0 9 0 3 0
  155. 0 0 5 0 0 0 0 4 0
  156. ).slices(9) if false
  157. func display_grid(grid) {
  158. for i in ^grid {
  159. print "#{grid[i]} "
  160. print " " if ( 3 -> divides(i+1))
  161. print "\n" if ( 9 -> divides(i+1))
  162. print "\n" if (27 -> divides(i+1))
  163. }
  164. }
  165. var solution = solve_sudoku(sudoku_board)
  166. if (solution) {
  167. display_grid(solution.flat)
  168. }
  169. else {
  170. warn "No unique solution exists."
  171. }