-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.rb
149 lines (127 loc) · 3.13 KB
/
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
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
require_relative 'pos'
require_relative 'sets'
Grid = Data.define(:rows, :cols, :boxes)
Sudoku = Struct.new(:grid, :bf, :pos)
Move = Data.define(:row, :col, :num)
def init_sudoku(rows)
cols = (0...9).map { |c| rows.map { |row| row[c] } }
boxes = Array.new(9) { Array.new(9) }
rows.each.with_index { |row, r|
row.each.with_index { |content, c|
box, i = rc2box(r, c)
boxes[box][i] = content
}
}
grid = Grid.new(rows, cols, boxes)
if (d = find_dup(grid.rows))
raise "duplicate in row #{d+1}"
end
if (d = find_dup(grid.cols))
raise "duplicate in column #{d+1}"
end
if (d = find_dup(grid.boxes))
raise "duplicate in box #{d+1}"
end
Sudoku.new(grid, 0, possible_positions(grid))
end
def find_dup(nums_arr)
nums_arr.find_index { |n|
n = n.filter { _1 != 0 }
n.uniq.size != n.size
}
end
def rc2box(r, c)
box = r / 3 * 3 + c / 3
i = r % 3 * 3 + c % 3
[box, i]
end
def deep_copy_sudoku(s)
new_grid = Grid.new(
s.grid.rows.map { |row| row.map(&:clone) },
s.grid.cols.map { |col| col.map(&:clone) },
s.grid.boxes.map { |box| box.map(&:clone) }
)
new_pos = s.pos.map { |row| row.map { |nums| nums&.map(&:clone) } }
Sudoku.new(new_grid, s.bf, new_pos)
end
def rc4box(box)
start_row = (box / 3) * 3
start_col = (box % 3) * 3
(start_row...start_row+3).to_a.product((start_col...start_col+3).to_a)
end
def move(s, m)
new = deep_copy_sudoku(s)
box, i = rc2box(m.row, m.col)
new.grid.rows[m.row][m.col] = m.num
new.grid.cols[m.col][m.row] = m.num
new.grid.boxes[box][i] = m.num
new.pos[m.row][m.col] = nil
new.pos[m.row].each.with_index { |_, i|
next unless new.pos[m.row][i]
new.pos[m.row][i] -= [m.num]
}
new.pos.each.with_index { |_, i|
next unless new.pos[i][m.col]
new.pos[i][m.col] -= [m.num]
}
rc4box(box).each { |r, c|
next unless new.pos[r][c]
new.pos[r][c] -= [m.num]
}
new
end
def done?(s)
s.grid.rows.all? { |row| row.all? { |num| num != 0 } }
end
def no_moves?(s)
s.pos.flatten.compact.empty?
end
def best_moves(s)
by_pos = best_by_position(s.pos)
return by_pos if by_pos.size == 1
[by_pos, best_by_sets(s.grid)].min_by { _1.size }
end
# Return rows matrix if solved, false otherwise
def solve_first(s)
return s if done?(s)
return nil if no_moves?(s)
moves = best_moves(s)
s.bf += (moves.size - 1)**2
moves.each do |move|
new = move(s, move)
solved = solve_first(new)
return solved if solved
# if the move is undone here, probably won't need to copy the whole sudoku
end
nil
end
def solve_all(s)
return [s] if done?(s)
return [] if no_moves?(s)
moves = best_moves(s)
s.bf += (moves.size - 1)**2
sols = []
moves.each do |move|
new = move(s, move)
sols += solve_all(new)
end
sols
end
def one_solution?(s)
search = ->(s, sols) do
return if sols.size > 1
return sols << s if done?(s)
return if no_moves?(s)
moves = best_moves(s)
s.bf += (moves.size - 1)**2
moves.each do |move|
new = move(s, move)
search.(new, sols)
break if sols.size > 1
end
end
sols = []
search.(s, sols)
return nil if sols.size != 1
sols.first
end