blob: 9ac93c059be09ace78b12f2b14bae5ec92789403 (
plain)
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
|
class Variable
def initialize(domain)
@domain = domain
end
def fixed?
if @domain.length == 1
@domain[0]
else
false
end
end
def include?(x)
@domain.include?(x)
end
def fix(x)
@domain = [x]
end
def eliminate(x)
@domain.delete(x) unless fixed?
end
end
def sudokuprint(grid, subsize, size = subsize * subsize)
w = Math.log10(size).ceil
bar = '┼ ┼' + (('─' * (subsize * (w + 1) + 1) + '┼') * subsize + ' ┼') * subsize
puts bar
puts bar
(0...size).step(subsize).each do |rs|
(rs...(rs + subsize)).each do |r|
(1..size).step(subsize).each do |ds|
print '│ │ '
(0...size).step(subsize).each do |b|
(b...(b + subsize)).each do |c|
(ds...(ds + subsize)).each do |d|
print (grid[r][c].include?(d) ? ("%#{w}d" % d) : (' ' * w)) + ' '
end
print '│ '
end
print '│ '
end
puts
end
puts bar
end
puts bar
end
end
SUBSIZE = 3
SIZE = SUBSIZE * SUBSIZE
boxes = []
(0...SIZE).step(SUBSIZE).each do |r|
(0...SIZE).step(SUBSIZE).each do |c|
boxes.push([r, c])
end
end
grid = Array.new(SIZE) {
Array.new(SIZE) {
Variable.new((1..SIZE).to_a)
}
}
grid[0][3].fix(2)
grid[0][4].fix(9)
grid[0][5].fix(4)
grid[0][6].fix(3)
grid[0][8].fix(5)
grid[1][0].fix(9)
grid[1][1].fix(5)
grid[1][3].fix(6)
grid[1][5].fix(8)
grid[1][6].fix(7)
grid[1][8].fix(2)
grid[2][2].fix(3)
grid[2][7].fix(8)
grid[3][1].fix(8)
grid[3][8].fix(4)
grid[4][1].fix(6)
grid[4][3].fix(9)
grid[4][5].fix(1)
grid[4][7].fix(7)
grid[5][0].fix(4)
grid[5][7].fix(5)
grid[6][1].fix(1)
grid[6][6].fix(2)
grid[7][0].fix(2)
grid[7][2].fix(6)
grid[7][3].fix(5)
grid[7][5].fix(3)
grid[7][7].fix(9)
grid[7][8].fix(8)
grid[8][0].fix(3)
grid[8][2].fix(7)
grid[8][3].fix(1)
grid[8][4].fix(8)
grid[8][5].fix(2)
until grid.flatten.count(&:fixed?) == 81
(0...SIZE).each do |i|
row = grid[i]
row.each do |cell|
if v = cell.fixed?
row.each do |c2|
c2.eliminate(v)
end
end
end
end
(0...SIZE).each do |i|
col = grid.map { |row| row[i] }
col.each do |cell|
if v = cell.fixed?
col.each do |c2|
c2.eliminate(v)
end
end
end
end
(0...SIZE).each do |i|
rs, cs = boxes[i]
box = []
(rs...(rs + SUBSIZE)).each do |r|
(cs...(cs + SUBSIZE)).each do |c|
box.push(grid[r][c])
end
end
box.each do |cell|
if v = cell.fixed?
box.each do |c2|
c2.eliminate(v)
end
end
end
end
end
sudokuprint(grid, SUBSIZE, SIZE)
|