-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsudoku_solver.rb
47 lines (42 loc) · 1.13 KB
/
sudoku_solver.rb
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
require "dlx"
class SudokuSolver
attr_reader :puzzle, :solutions
def initialize(puzzle)
@dlx = DLX.new(324)
@puzzle = puzzle
@solutions = []
end
def solve
setup_sparse_matrix
@dlx.solve
convert_solutions
end
private
def setup_sparse_matrix
9.times do |row|
9.times do |column|
1.upto 9 do |digit|
box = (row / 3).floor * 3 + (column / 3).floor
dlx_row = [row*9 + column + 1]
dlx_row << 81 + row*9 + digit
dlx_row << 81 + 81 + column*9 + digit
dlx_row << 81 + 81 + 81 + box*9 + digit
@dlx.add_row(dlx_row) unless( @puzzle[row][column] > 0 && @puzzle[row][column] != digit )
end
end
end
end
def convert_solutions
return if @dlx.solutions.size == 0
solution_matrix = [[],[],[],[],[],[],[],[],[]]
@dlx.solutions.each_with_index do |solution,index|
solution.each do |dlx_row|
dlx_row.sort!
row = ((dlx_row[0]-1)/9).floor
column = (dlx_row[0]-1)%9
solution_matrix[row][column] = dlx_row[1]-81-(row*9)
end
@solutions << solution_matrix
end
end
end