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
use sudoku::Grid;
use Element;
use Point;
use Sudoku;
use DIMENSIONS;
use std::ops::{Index, IndexMut};
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub enum Difficulty {
#[doc(hidden)]
Unplayable,
Beginner,
Easy,
Intermediate,
Difficult,
Advanced,
}
impl From<usize> for Difficulty {
fn from(score: usize) -> Self {
use Difficulty::*;
match score {
0...49 => Unplayable,
50...150 => Beginner,
151...250 => Easy,
251...400 => Intermediate,
401...550 => Difficult,
_ => Advanced,
}
}
}
#[derive(Clone, Debug)]
#[allow(missing_copy_implementations)]
pub enum Error {
Unknown,
#[doc(hidden)]
__TestOther,
}
pub trait Solve: Sized {
fn solution(&self) -> Result<Self, Error>;
fn is_uniquely_solvable(&self) -> bool {
self.solution().is_ok()
}
}
pub trait Score: Solve {
fn score(&self) -> Option<usize>;
fn difficulty(&self) -> Option<Difficulty> {
self.score().map(|s| s.into())
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PossibilitySet {
pub values: u64,
}
impl PossibilitySet {
pub fn new(order: u8) -> Self {
let mut values = 0;
for i in 1..=order.pow(2) as usize {
values |= 1 << (i - 1);
}
Self { values }
}
pub fn eliminate(&self, value: usize) -> Option<Self> {
let values = self.values & !(1 << (value - 1));
match values {
0 => None,
_ => Some(Self { values }),
}
}
pub fn freedom(&self) -> usize {
let mut x = self.values;
let mut n = 0;
while x > 0 {
x &= x - 1;
n += 1;
}
n
}
pub fn contains(&self, value: usize) -> bool {
self.values | (1 << (value - 1)) == self.values
}
}
#[derive(Debug)]
pub struct PossibilityMap {
possibilities: Vec<Option<PossibilitySet>>,
order: u8,
}
impl PossibilityMap {
pub fn new(order: u8) -> Self {
Self {
possibilities: vec![
Some(PossibilitySet::new(order));
(order as usize).pow(2 + DIMENSIONS as u32)
],
order,
}
}
pub fn eliminate(&mut self, index: Point, value: usize) {
self[index] = self[index].and_then(|e| e.eliminate(value));
}
pub fn next(&self) -> (Option<Point>, Option<PossibilitySet>) {
let mut best = None;
let mut best_index = None;
let mut best_score = None;
for index in self.points() {
if let Some(element) = self[index] {
if best_score.is_none() || best_score.unwrap() > element.freedom() {
best = Some(element);
best_index = Some(index);
best_score = Some(element.freedom());
}
}
}
(best_index, best)
}
}
impl Index<Point> for PossibilityMap {
type Output = Option<PossibilitySet>;
fn index(&self, index: Point) -> &Self::Output {
let index = index.fold(self.order);
&self.possibilities[index]
}
}
impl IndexMut<Point> for PossibilityMap {
fn index_mut(&mut self, index: Point) -> &mut Option<PossibilitySet> {
let index = index.fold(self.order);
&mut self.possibilities[index]
}
}
impl Grid for PossibilityMap {
fn points(&self) -> Vec<Point> {
(0..(self.order as usize).pow(2 + DIMENSIONS as u32))
.map(|p| Point::unfold(p, self.order))
.collect()
}
}
impl From<Sudoku> for PossibilityMap {
fn from(sudoku: Sudoku) -> Self {
let order = sudoku.order;
let mut map = PossibilityMap::new(order);
for i in 0..(sudoku.order as usize).pow(2 + DIMENSIONS as u32) {
let point = Point::unfold(i, order);
if sudoku[point].is_some() {
map[point] = None;
} else {
let groups = sudoku.groups(point);
for group in groups.iter() {
let elements = group.elements();
for element in elements {
match element {
Some(Element(value)) => {
map.eliminate(point, value as usize);
}
None => {}
}
}
}
}
}
map
}
}
pub fn solve(puzzle: &Sudoku) -> Result<Sudoku, Error> {
solve_and_score(puzzle).map(|(sol, _)| sol)
}
pub fn solve_and_score(puzzle: &Sudoku) -> Result<(Sudoku, usize), Error> {
let mut context = Context {
problem: puzzle.clone(),
count: 0,
solution: None,
branch_score: 0,
};
recurse(&mut context, 0);
let s = context.branch_score;
let c = calculate_c(puzzle) as isize;
let e = count_empty(puzzle) as isize;
context
.solution
.ok_or(Error::Unknown)
.map(|sol| (sol, (s * c + e) as usize))
}
struct Context {
problem: Sudoku,
count: usize,
solution: Option<Sudoku>,
branch_score: isize,
}
fn recurse(mut context: &mut Context, difficulty: isize) {
let problem = context.problem.clone();
let map: PossibilityMap = problem.into();
match map.next() {
(None, _) => {
if context.problem.is_complete() {
if context.count == 0 {
context.branch_score = difficulty;
context.solution = Some(context.problem.clone());
}
context.count += 1;
}
return;
}
(Some(index), Some(set)) => {
let branch_factor = set.freedom() as isize - 1;
let possible = (1..=(context.problem.order as usize).pow(2))
.filter(|v| set.contains(*v))
.collect::<Vec<_>>();
let difficulty = difficulty + branch_factor.pow(DIMENSIONS as u32);
for value in possible {
let problem = context
.problem
.substitute(index, Some(Element(value as u8)));
context.problem = problem;
recurse(&mut context, difficulty);
if context.count > 1 {
return;
}
}
context.problem = context.problem.substitute(index, None);
}
_ => unreachable!(),
}
}
fn count_empty(sudoku: &Sudoku) -> usize {
sudoku
.elements
.iter()
.filter(|e| e.is_none())
.collect::<Vec<_>>()
.len()
}
fn calculate_c(sudoku: &Sudoku) -> usize {
let order = sudoku.order;
10.0_f64.powf((order as f64).powf(4.0).log10().ceil()) as usize
}
pub fn score(sudoku: &Sudoku) -> Option<usize> {
solve_and_score(&sudoku).ok().map(|(_, s)| s)
}
#[cfg(test)]
mod tests {
use sol::{calculate_c, Error, PossibilityMap, PossibilitySet, Solve};
use Point;
use Sudoku;
use DIMENSIONS;
struct DummyPuzzle(bool);
impl DummyPuzzle {
fn new(solvable: bool) -> Self {
Self { 0: solvable }
}
}
impl Solve for DummyPuzzle {
fn solution(&self) -> Result<Self, Error> {
if self.0 {
Ok(Self { 0: true })
} else {
Err(Error::__TestOther)
}
}
}
#[test]
fn test_is_uniquely_solvable() {
let solvable = DummyPuzzle::new(true);
assert_eq!(solvable.is_uniquely_solvable(), true);
let unsolvable = DummyPuzzle::new(false);
assert_eq!(unsolvable.is_uniquely_solvable(), false);
}
#[test]
fn test_calculate_c() {
let sudoku = Sudoku::new(3);
assert_eq!(calculate_c(&sudoku), 100);
let sudoku = Sudoku::new(4);
assert_eq!(calculate_c(&sudoku), 1_000);
let sudoku = Sudoku::new(5);
assert_eq!(calculate_c(&sudoku), 1_000);
let sudoku = Sudoku::new(6);
assert_eq!(calculate_c(&sudoku), 10_000);
}
#[test]
fn test_map_new() {
for order in 1..6 {
let map = PossibilityMap::new(order);
for i in 0..(order as usize).pow(DIMENSIONS as u32 + 2) {
let index = Point::unfold(i, order);
let set = PossibilitySet::new(order);
assert_eq!(map[index], Some(set));
}
}
}
#[test]
fn test_map_from_sudoku() {
let sudoku = Sudoku::new(3);
let map: PossibilityMap = sudoku.into();
for p in map.possibilities {
assert_eq!(p, Some(PossibilitySet::new(3)));
}
}
#[test]
fn test_set_new() {
let set = PossibilitySet::new(3);
for i in 1..10 {
assert!(set.contains(i));
}
}
#[test]
fn test_set_eliminate() {
let mut set = PossibilitySet::new(3);
for i in 1..9 {
set = set.eliminate(i).unwrap();
assert!(!set.contains(i));
}
assert_eq!(set.eliminate(9), None);
}
#[test]
fn test_set_freedom() {
let mut set = PossibilitySet::new(3);
for i in 1..9 {
set = set.eliminate(i).unwrap();
assert_eq!(set.freedom(), 9 - i);
}
}
}