-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.rb
433 lines (358 loc) · 8.96 KB
/
sudoku.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
require 'set'
# a Sudoku Grid
# Holds the actual data values
class Grid
CELL_RANGE = (0...9*9)
ROW_RANGE = (0...9)
COLUMN_RANGE = (0...9)
BOX_RANGES = [[0..2, 0..2], [0..2, 3..5], [0..2, 6..8],
[3..5, 0..2], [3..5, 3..5], [3..5, 6..8],
[6..8, 0..2], [6..8, 3..5], [6..8, 6..8]]
attr_reader :cells, :rows, :columns, :boxes
def initialize
@cells = CELL_RANGE.map { |idx| Cell.new(*idx_to_xy(idx)) }
@rows = ROW_RANGE.map { |row| Row.new(@cells.select { |cell| cell.x == row }) }
@columns = COLUMN_RANGE.map { |column| Column.new(@cells.select { |cell| cell.y == column }) }
@boxes = BOX_RANGES.map { |v|
Box.new(v, @cells.select { |cell|
v[0] === cell.x and v[1] === cell.y
})
}
end
def cell_at(x, y)
@cells[xy_to_idx(x, y)]
end
def row(y)
@rows[y]
end
def column(x)
@columns[x]
end
def box_for(x, y)
@boxes.select { |box| box.x_range === x and box.y_range === y }.first
end
# From any cell in this cells row/col/box, remove the cell's
# value from the possible list
def adjust_possibles(cell)
row(cell.x).cells.each { |c|
c.remove_possible(cell.value)
}
column(cell.y).cells.each { |c|
c.remove_possible(cell.value)
}
box_for(cell.x, cell.y).cells.each { |c|
c.remove_possible(cell.value)
}
end
def pretty_print
row_sep = '++---+---+---++---+---+---++---+---+---++'
row = -1
@cells.each { |cell|
if row != cell.x
printf("\n%s\n", (cell.x % 3 == 0) ? row_sep.gsub(/-/, '=') : row_sep)
printf '||'
row = cell.x
end
printf(' %s %s', cell.value.nil? ? ' ' : cell.value.to_s, (cell.y % 3 == 2) ? '||' : '|')
}
printf("\n%s\n\n", row_sep.gsub(/-/, '='))
end
def xy_to_idx(x, y)
x * 9 + y
end
def idx_to_xy(idx)
[(idx / 9), (idx % 9)]
end
end
# a single Cell
class Cell
attr_reader :x, :y, :possible, :value
def initialize(*args)
@value = nil
@possible = Set.new
@x = args[0]
@y = args[1]
end
def value=(val)
@value = val
@possible = Set.new
end
def clear_possible
@possible = Set.new
end
def add_possible(val)
@possible << val
end
def remove_possible(val)
@possible.delete(val)
end
# Useful if we want to test a case by creating a sample grid
# with sample possible values.
def debug_possible(vals)
@possible = Set.new(vals)
end
end
module CellCollection
def include?(val)
@cells.select { |cell| cell.value == val }.length > 0
end
def possible_cells_for(val)
@cells.select { |cell| cell.possible.include?(val) }
end
end
# a 3x3 Cell Box
class Box
ROW_RANGE = (0...3)
COLUMN_RANGE = (0...3)
attr_reader :x_range, :y_range, :cells, :rows, :columns
include CellCollection
def initialize(*args)
@x_range = args[0][0]
@y_range = args[0][1]
@cells = args[1]
@rows = ROW_RANGE.map { |row| Row.new(@cells.select { |cell| row + @x_range.first == cell.x }) }
@columns = COLUMN_RANGE.map { |column| Column.new(@cells.select { |cell| column + @y_range.first == cell.y }) }
end
# this operation works relative to the top of the box.
# i.e. box 2 (top middle box) cell 0,0 is actually grid cell 0,3
def cell_at(x, y)
@cells.select { |cell| cell.x == @x_range.first + x and cell.y == @y_range.first + y }.first
end
end
class Row
attr_accessor :cells
include CellCollection
def initialize(cells)
@cells = cells
end
# Returns a set of all the possible values which can
# occupy this row
def possible
@cells.inject(Set.new) { |row_set, cell|
row_set.merge(cell.possible)
}
end
end
Column = Row # Column Class == Row Class
class Solver
def initialize(grid)
@grid = grid
end
def solve
initialize_possibles
values_set, last = -1, 0
while values_set != last
last = values_set
# Possible Removers
naked_pairs
locked_candidate_1
# Value Setters
hidden_singles
singles
values_set = @grid.cells.select { |c| !c.value.nil? }.length
end
end
def initialize_possibles
@grid.cells.each { |cell|
if cell.value.nil?
(1..9).each { |val|
if !@grid.row(cell.x).include?(val) \
and !@grid.column(cell.y).include?(val) \
and !@grid.box_for(cell.x, cell.y).include?(val)
cell.add_possible(val)
end
}
end
}
end
# assign a value to a cell with only 1 possible
def singles
@grid.cells.each { |cell|
if cell.possible.length == 1
#p "found a single"
cell.value = cell.possible.to_a.first
@grid.adjust_possibles(cell)
end
}
end
# assign a value if a box, row or column only has 1 possible for a value
def hidden_singles
fn = lambda { |collection|
(1..9).map { |val| {:val => val, :cells => collection.possible_cells_for(val)} }.select { |hash| hash[:cells].length == 1}.each { |v|
v[:cells].each { |cell|
#p "found a hidden signal"
cell.value = v[:val]
@grid.adjust_possibles(cell)
}
}
}
@grid.rows.each(&fn)
@grid.columns.each(&fn)
@grid.boxes.each(&fn)
end
def naked_pairs
fn = lambda { |collection|
pairs = Set.new
collection.cells.each { |cell|
if cell.possible.length == 2
collection.cells.each { |cell2|
# If they contain the same 2 candidates
if cell.possible == cell2.possible and !cell.equal?(cell2)
#p "Found a naked pair"
pairs << cell.possible
end
}
end
}
pairs.each { |pair|
collection.cells.each { |cell|
if pair != cell.possible
pair.each { |v|
cell.remove_possible(v)
}
end
}
}
}
@grid.rows.each(&fn)
@grid.columns.each(&fn)
@grid.boxes.each(&fn)
end
def locked_candidate_1
# for each column/row in a box, look for
# values which only appear in that col/row of the box.
# we can then remove these values from every other cell
# in the row/col (out of the box)
@grid.boxes.each { |box|
# ROWS
box.rows.each { |row|
diff_set = row.possible
box.rows.each { |row2|
if !row.equal?(row2)
diff_set = diff_set - row2.possible
end
}
# Diff set contains a list of values in this box row which
# are not in any other row in the box. we can remove them
# from the cells in the row that are outside of the box
@grid.row(row.cells.first.x).cells.each { |cell|
if !row.cells.include?(cell)
diff_set.each { |v|
#p "Removing Locked Candidate"
cell.remove_possible(v)
}
end
}
}
# COLUMNS
box.columns.each { |column|
diff_set = column.possible
box.columns.each { |column2|
if !column.equal?(column2)
diff_set = diff_set - column2.possible
end
}
# Diff set contains a list of values in this box column which
# are not in any other column in the box. we can remove them
# from the cells in the column that are outside of the box
@grid.column(column.cells.first.y).cells.each { |cell|
if !column.cells.include?(cell)
diff_set.each { |v|
#p "Removing Locked Candidate"
cell.remove_possible(v)
}
end
}
}
}
end
end
g = Grid.new
g.cell_at(0,1).value = 7
g.cell_at(0,6).value = 8
g.cell_at(1,3).value = 2
g.cell_at(1,5).value = 4
g.cell_at(2,2).value = 6
g.cell_at(2,7).value = 3
g.cell_at(3,3).value = 5
g.cell_at(3,8).value = 6
g.cell_at(4,0).value = 9
g.cell_at(4,2).value = 8
g.cell_at(4,5).value = 2
g.cell_at(4,7).value = 4
g.cell_at(5,1).value = 5
g.cell_at(5,4).value = 3
g.cell_at(5,6).value = 9
g.cell_at(6,2).value = 2
g.cell_at(6,4).value = 8
g.cell_at(6,7).value = 6
g.cell_at(7,1).value = 6
g.cell_at(7,3).value = 9
g.cell_at(7,6).value = 7
g.cell_at(7,8).value = 1
g.cell_at(8,0).value = 4
g.cell_at(8,5).value = 3
=begin
g.cell_at(1,2).value = 7
g.cell_at(1,3).value = 8
g.cell_at(1,4).value = 3
g.cell_at(1,6).value = 9
g.cell_at(2,2).value = 5
g.cell_at(2,5).value = 2
g.cell_at(2,6).value = 6
g.cell_at(2,7).value = 4
g.cell_at(3,2).value = 2
g.cell_at(3,3).value = 6
g.cell_at(3,7).value = 7
g.cell_at(4,1).value = 4
g.cell_at(4,7).value = 8
g.cell_at(5,1).value = 6
g.cell_at(5,5).value = 3
g.cell_at(5,6).value = 2
g.cell_at(6,1).value = 2
g.cell_at(6,2).value = 8
g.cell_at(6,3).value = 4
g.cell_at(6,6).value = 5
g.cell_at(7,4).value = 9
g.cell_at(7,5).value = 6
g.cell_at(7,6).value = 1
=end
=begin
# Should find a 9 in 0,0 through single
g.cell_at(1,0).value = 1
g.cell_at(2,0).value = 2
g.cell_at(3,0).value = 3
g.cell_at(4,0).value = 4
g.cell_at(5,0).value = 5
g.cell_at(6,0).value = 6
g.cell_at(7,0).value = 7
g.cell_at(8,0).value = 8
=end
=begin
g.cell_at(1,3).value = 1
g.cell_at(2,6).value = 1
g.cell_at(3,1).value = 1
g.cell_at(6,2).value = 1
=end
=begin
# Values, then possibles
g.cell_at(0,0).value = 7
g.cell_at(0,4).value = 9
g.cell_at(0,8).value = 3
g.cell_at(0,1).debug_possible([1,2,4,5])
g.cell_at(0,2).debug_possible([1,4,5])
g.cell_at(0,3).debug_possible([2,4,5])
g.cell_at(0,5).debug_possible([8,6])
g.cell_at(0,6).debug_possible([8,6])
g.cell_at(0,7).debug_possible([1,6,8])
=end
print "=======================\n"
print " Beginning With"
print "=======================\n"
g.pretty_print
s = Solver.new(g)
s.solve
print "=======================\n"
print " Ending With\n"
print "=======================\n"
g.pretty_print