-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_cube.rb
398 lines (355 loc) · 7.95 KB
/
sudoku_cube.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
require 'set'
class Cube
class Face
def initialize(face=nil)
if face.nil?
s = Set.new(0..15)
@state = [[s.clone, s.clone, s.clone, s.clone],
[s.clone, s.clone, s.clone, s.clone],
[s.clone, s.clone, s.clone, s.clone],
[s.clone, s.clone, s.clone, s.clone]]
@orientation = 0
else
@state = face.state.clone
@orientation = face.orientation
end
end
attr_reader :state
attr_reader :orientation
def display_strings(orientation=0,fixed=true)
[ '/-------\\',
*display_elements(elements(orientation).map {|x| format_cell(fixed, x)} ),
'\\-------/']
end
def display_elements(elements)
["|#{elements.shift} #{elements.shift} #{elements.shift} #{elements.shift}|",
"|#{elements.shift} #{elements.shift} #{elements.shift} #{elements.shift}|",
"|#{elements.shift} #{elements.shift} #{elements.shift} #{elements.shift}|",
"|#{elements.shift} #{elements.shift} #{elements.shift} #{elements.shift}|"]
end
def elements(orientation)
res = []
orientation = (orientation + @orientation) % 4
(0..3).each { |r| res.push(*get_row(r)) }
res
end
def format_cell(fixed, a_cell)
if fixed
format_cell_fixed(a_cell)
else
format_cell_pos(a_cell)
end
end
def format_cell_fixed(a_cell)
case a_cell.size
when 0
'!'
when 1
a_cell.to_a[0].to_s(16)
else
'?'
end
end
def format_cell_pos(a_cell)
case a_cell.size
when 0
'!'
when 1
'x'
when 16
'*'
else
a_cell.size.to_s(16)
end
end
def rotate(n=1)
@orientation = (@orientation + n) % 4
self
end
def get_cell(r, c)
get_row(r)[c]
end
def get_row(n, orientation=0)
orientation = (orientation + @orientation) % 4
case orientation
when 0
@state[n]
when 1
(0..3).map { |x| @state[3 - x][n] }.to_a
when 2
@state[3 - n].reverse
when 3
(0..3).map { |x| @state[x][3 - n] }.to_a
end
end
def get_col(n, orientation=0)
orientation = (orientation + @orientation) % 4
case orientation
when 0
(0..3).map { |x| @state[x][n] }.to_a
when 1
@state[3 - n].reverse
when 2
(0..3).map { |x| @state[3 - x][3 - n] }.to_a
when 3
@state[n]
end
end
def to_s(orientation=0, fixed=true)
display_strings(orientation, fixed).reduce { |a, b| a + "\n" + b }
end
def inspect
to_s
end
end
def initialize
@f = Face.new
@b = Face.new
@u = Face.new
@d = Face.new
@l = Face.new
@r = Face.new
end
def to_s(fixed=true)
us = @u.display_strings(0, fixed)
ds = @d.display_strings(0, fixed)
fs = @f.display_strings(0, fixed)
bs = @b.display_strings(0, fixed)
ls = @l.display_strings(0, fixed)
rs = @r.display_strings(0, fixed)
6.times { puts " " + us.shift }
6.times { puts ls.shift + ' ' + fs.shift + ' ' + rs.shift + ' ' + bs.shift }
6.times { puts " " + ds.shift }
end
def get_linked_rows(r,c)
res = []
[@f, @l, @r, @b].each {|f| f.get_row(r).each { |e| res << e } }
res.delete_at(c)
res
end
def get_linked_cols(r,c)
res = []
[@f, @u, @d].each {|f| f.get_col(c).each { |e| res << e } }
@b.get_col(3 - c).each { |e| res << e }
res.delete_at(r)
res
end
def get_other_face_cells(r,c)
res = []
(0..3).each {|x| @f.get_row(x).each { |e| res << e } }
res.delete_at( r * 4 + c )
res
end
def set(r, c, val)
cell = @f.get_cell(r, c)
cell.reject! { |x| x != val }
[ get_linked_rows(r,c), get_linked_cols(r,c), get_other_face_cells(r,c)].each do |linked|
linked.each { |c| c.delete(val) }
end
self
end
def check(r, c, val)
cell = @f.get_cell(r, c).clone
if !cell.include?(val)
return false
end
res = true
[ get_linked_rows(r,c), get_linked_cols(r,c), get_other_face_cells(r,c)].each do |linked|
res = res & check_linked(linked, val)
end
res
end
def check_linked(linked, val)
vals = Set.new
vals.add (val)
linked.each do |cell|
cell = cell.clone
if cell.size == 1 && cell.include?(val)
return false
end
cell.delete(val)
if cell.size == 1
if vals.include?(cell.to_a[0])
return false
else
vals.add(cell.to_a[0])
end
end
end
true
end
def possible(r, c)
cell = @f.get_cell(r, c).clone
end
def rotate_f_to_l(n=1)
n.times do
tmp = @l
@l = @f
@f = @r
@r = @b
@b = tmp
@u.rotate
@d.rotate(-1)
end
self
end
def rotate_u_to_f(n=1)
n.times do
tmp = @f
@f = @u
@u = @b
@b = @d
@d = tmp
@u.rotate(2)
@b.rotate(2)
@l.rotate
@r.rotate(-1)
end
self
end
def rotate_clockwise
rotate_f_to_l
rotate_u_to_f
rotate_f_to_l(3)
end
def self.generate()
seed = nil
res = nil
count = 0
c = nil
stats = Array.new(97,0)
while(seed == nil)
c = Cube.new
c.set_face(ORDERED_SQUARE)
(seed, res) = c.construct
stats[c.fixed_cells] += 1
count += 1
if (count % 1000) == 0
puts "#{count} attempts."
end
end
puts "Cube #{seed} after #{count} attempts."
c
end
def fixed_cells
total = 0
[@l, @f, @r, @b, @u, @d].each { |f|
(0..3).each { |r|
row = f.get_row(r)
(0..3).each { |c|
total += 1 if row[c].size == 1
}
}
}
total
end
def self.gen_test(seed=nil)
seed = Random.new.seed if seed.nil?
c = Cube.new
c.construct(seed)
puts "Seed = #{seed}"
c
end
def construct(seed=nil)
r = if seed.nil?
Random.new
else
Random.new(seed)
end
seed = r.seed
magic_square
return [nil, nil] unless construct_faces(r)
rotate_u_to_f
return [nil, nil] unless construct_faces(r)
[seed, self]
end
def construct_faces(prng)
(0..3).each do
return false unless construct_face(prng)
rotate_f_to_l
end
true
end
def construct_face(prng)
(0..3).each do |r|
(0..3).each do |c|
pos = possible(r,c).to_a
case pos.size
when 0
return false
when 1
next
else
while (pos.size > 0)
v = prng.rand(pos.size)
if check(r, c, pos[v])
set(r, c, pos[v])
break
else
pos.delete_at(v)
end
if pos.size == 1 && !check(r,c, pos[0])
return false
end
end
end
end
end
true
end
MAGIC_SQUARE = [[0, 13, 7, 10],
[14, 3, 9, 4],
[11, 6, 12, 1],
[5, 8, 2, 15]]
ORDERED_SQUARE = [[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15, 16]]
def all_orientations
4.times do
yield
rotate_clockwise
end
end
def all_faces
4.times do
yield
rotate_f_to_l
end
rotate_u_to_f
yield
rotate_u_to_f(2)
yield
end
def attempt_set_face(square)
all_faces do
all_orientations do
return true if check_face(square)
end
end
false
end
def set_face(square)
(0..3).each do |r|
(0..3).each do |c|
if check(r, c, square[r][c])
set(r, c, square[r][c])
end
end
end
self
end
def check_face(square)
(0..3).each do |r|
(0..3).each do |c|
unless check(r, c, square[r][c])
return false
end
end
end
true
end
def inspect
to_s
end
end