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