summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 0e105d16bb6630777d1ba8163f57205774907a4b (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
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
use std::fmt;
use std::io;
use std::io::prelude::*;
use std::ops::{Index, IndexMut, Range};
use std::process::exit;
use std::vec::Vec;
//use std::collections::HashMap;

#[inline]
fn fixed_point<T, F>(x: T, f: F) -> T
    where T: Eq + Clone, F: Fn(T) -> T
{
    let n = f(x.clone());

    if n == x {
        x
    } else {
        fixed_point(n, f)
    }
}

trait FoldWhile: Iterator {
    #[inline]
    fn fold_while<T, F>(self, init: T, mut f: F) -> Option<T> where
        Self: Sized, F: FnMut(T, Self::Item) -> Option<T>,
    {
        let mut accum = init;
        for x in self {
            accum = match f(accum, x) {
                Some(a)     => a,
                None        => return None
            }

        }
        Some(accum)
    }
}

impl<I> FoldWhile for I where I: Iterator {}

#[derive(Eq, PartialEq, Copy, Clone)]
struct BitBoardPos {
    x:          usize,
    y:          usize,
}

struct EachPos {
    i:          Range<usize>,
}

impl Iterator for EachPos {
    type Item = BitBoardPos;

    fn next(&mut self) -> Option<BitBoardPos> {
        self.i.next().map(|i| BitBoardPos { x: i % 9, y: i / 9 })
    }
}

fn each_pos() -> EachPos {
    EachPos { i: (0..81) }
}

struct EachBlock {
    i:          Range<usize>,
}

impl Iterator for EachBlock {
    type Item = BitBoardPos;

    fn next(&mut self) -> Option<BitBoardPos> {
        self.i.next().map(|i| BitBoardPos { x: i / 3 * 3, y: i % 3 * 3 })
    }
}

fn each_block() -> EachBlock {
    EachBlock { i: (0..9) }
}

struct Column {
    x:          usize,
    y:          Range<usize>,
}

impl Iterator for Column {
    type Item = BitBoardPos;

    fn next(&mut self) -> Option<BitBoardPos> {
        self.y.next().map(|y| BitBoardPos { x: self.x, y: y })
    }
}

struct Row {
    x:          Range<usize>,
    y:          usize,
}

impl Iterator for Row {
    type Item = BitBoardPos;

    fn next(&mut self) -> Option<BitBoardPos> {
        self.x.next().map(|x| BitBoardPos { x: x, y: self.y })
    }
}

struct Block {
    start:      BitBoardPos,
    i:          Range<usize>,
}

impl Iterator for Block {
    type Item = BitBoardPos;

    fn next(&mut self) -> Option<BitBoardPos> {
        self.i.next().map(|i| BitBoardPos {
            x: self.start.x + i / 3,
            y: self.start.y + i % 3
        })
    }
}

impl BitBoardPos {
    fn col(self) -> Column {
        Column  { x: self.x, y: (0..9), }
    }

    fn row(self) -> Row {
        Row     { x: (0..9), y: self.y, }
    }

    fn block(self) -> Block {
        Block {
            start: BitBoardPos {
                x: self.x / 3 * 3,
                y: self.y / 3 * 3,
            },
            i: (0..9),
        }
    }

    fn blocknr(self) -> usize {
        self.x / 3  * 3 + self.y / 3
    }
}

const  ALL_NUMS: u16 = ((1 << 9) - 1) << 1;

#[derive(Eq, PartialEq, Hash, Copy, Clone)]
struct BitBoard {
    b:  [[u16; 9]; 9],
}

impl BitBoard {
    fn new() -> BitBoard {
        BitBoard { b: [[ALL_NUMS; 9]; 9], }
    }

    fn pos_known(&self, p: BitBoardPos) -> bool {
        self[p].is_power_of_two()
    }

    fn nr_known(&self) -> usize {
        each_pos()
            .filter(|p| self.pos_known(*p))
            .count()
    }

    fn nr_choices(&self) -> u32 {
        each_pos().fold(0, |a, i| a + self[i].count_ones())
    }

    fn clear(&self, p: BitBoardPos, v: u16) -> BitBoard {
        let mut b = *self;
        b[p] &= v ^ ALL_NUMS;
        b
    }

    fn set(&self, p: BitBoardPos, v: u16) -> BitBoard {
        let mut b = *self;
        b[p] &= v;
        b
    }
}

impl Index<BitBoardPos> for BitBoard {
    type Output = u16;

    fn index<'a>(&'a self, p: BitBoardPos) -> &'a u16 {
        &self.b[p.x][p.y]
    }
}

impl IndexMut<BitBoardPos> for BitBoard {
    fn index_mut<'a>(&'a mut self, p: BitBoardPos) -> &'a mut u16 {
        &mut self.b[p.x][p.y]
    }
}

fn read_board() -> BitBoard {
    let mut b = BitBoard::new();

    for i in 0..9 {
        let mut line = String::new();

        if io::stdin().read_line(&mut line).unwrap() < 9 {
            println!("didn't get sudoku line");
            exit(1);
        }

        for (j, c) in line.chars().enumerate() {
            if c == '\n' {
                break;
            }

            b.b[i][j] = if c == '.' {
                ALL_NUMS
            } else {
                1 << ((c as u32) - ('0' as u32)) as u16
            }
        }
    }

    b
}

impl fmt::Display for BitBoard {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for p in each_pos() {
            if p.x == 0 && p.y != 0 && p.y % 3 == 0 {
                try!(write!(f, "-------|-------|-------\n"));
            }

            if p.x != 0 && p.x % 3 == 0 {
                try!(write!(f, " |"));
            }

            if self.pos_known(p) {
                try!(write!(f, " {}", self[p].trailing_zeros()));
            } else {
                try!(write!(f, " ."));
            }

            if p.x == 8 {
                try!(write!(f, "\n"));
            }
        }

        Ok(())
    }
}

/*
 * Check if a board configuration is valid: not actually used in the solver,
 * just an additional check:
 */
fn check(b: &BitBoard) -> bool {
    let origin = BitBoardPos { x: 0, y: 0 };

    /* Check a column/row/block for duplicates - true if no duplicates */
    fn check_iter(b: &BitBoard, iter: &mut Iterator<Item=BitBoardPos>) -> bool {
        iter.filter(|i| b.pos_known(*i))
            .fold_while(0u16, |v, i|
                        if v & b[i] != 0 { None }
                        else { Some(v | b[i]) })
            .is_some()
    }

    origin.row().all(|i| check_iter(b, &mut i.col())) &&
    origin.col().all(|i| check_iter(b, &mut i.row())) &&
    each_block().all(|i| check_iter(b, &mut i.block()))
}

/* Start of solver: */

/*
 * Given a position that we know the answer for, strike that number as a possibility from every
 * other position in the same row, column and block:
 */
fn strike_solved(mut b: BitBoard, p: BitBoardPos) -> Option<BitBoard> {
    /*
     * if @p is solved, strike it from every other cell in the same
     * row/column/block:
     */
    if b.pos_known(p) {
        for i in p.col()
            .chain(p.row())
            .chain(p.block())
            .filter(|i| *i != p) {
            b = b.clear(i, b[p]);

            if b[i] == 0 {
                println!("inconsistent at:\n{}", b);
                return None;
            }
        }
    }

    Some(b)
}

/*
 * Check every other cell in the same row/column/block as this cell - if
 * there is a number that we've determined can't be in any of those cells,
 * then it must be in this cell:
 */
fn constrain_single(mut b: BitBoard, p: BitBoardPos,
                    iter: &mut Iterator<Item=BitBoardPos>) -> Option<BitBoard> {
    if !b.pos_known(p) {
        let m = iter
            .filter(|i| *i != p)
            .fold(0u16, |a, i| a | b[i])
            ^ ALL_NUMS;

        if m.is_power_of_two() {
            b = b.set(p, m);

            if b[p] == 0 {
                println!("inconsistent at:\n{}", b);
                return None;
            }
        }
    }

    Some(b)
}

fn constrain_multiple(b: BitBoard, p: BitBoardPos) -> Option<BitBoard> {
    let mut b = b;

    if !b.pos_known(p) {
        let mut other_rows = 0u16;
        let mut other_cols = 0u16;

        for i in p.block() {
            if i.x != p.x {
                other_cols |= b[i];
            }

            if i.y != p.y {
                other_rows |= b[i];
            }
        }

        for i in p.col().filter(|i| i.blocknr() != p.blocknr()) {
            b = b.set(i, other_cols);

            if b[i] == 0 {
                println!("inconsistent at:\n{}", b);
                return None;
            }
        }

        for i in p.row().filter(|i| i.blocknr() != p.blocknr()) {
            b = b.set(i, other_rows);

            if b[i] == 0 {
                println!("inconsistent at:\n{}", b);
                return None;
            }
        }
    }

    Some(b)
}

fn solve(b: BitBoard) -> Vec<BitBoard> {
    /*
     * Try to solve with the various constraint solvers until we hit a fixed point - until we
     * either get stuck, or discover that we were inconsistent (because we were guessing in the
     * backtracking solver:
     *
     * The nested fixed point iterations try the cheaper solvers first:
     */
    let b = match fixed_point(Some(b), |b| {
        let b = fixed_point(b, |b| {
            let b = fixed_point(b, |b| {
                b.and_then(|b| each_pos().fold_while(b, |b, i| strike_solved(b, i)))
            });

            b.and_then(|b| each_pos().fold_while(b, |b, i| constrain_single(b, i, &mut i.col())))
             .and_then(|b| each_pos().fold_while(b, |b, i| constrain_single(b, i, &mut i.row())))
             .and_then(|b| each_pos().fold_while(b, |b, i| constrain_single(b, i, &mut i.block())))
        });

        b.and_then(|b| each_pos().fold_while(b, |b, i| constrain_multiple(b, i)))
    }) {
        Some(b) => b,
        None    => return Vec::new(),
    };

    /*
     * When we get stuck, if there's unsolved cells left switch to backtracking:
     */
    match each_pos().find(|p| !b.pos_known(*p)) {
        None        => vec![b],
        Some(p)     => {
            println!("backtracking ({} solved {} choices):\n{}", b.nr_known(), b.nr_choices(), b);

            (1..10)
                .filter(|i| (b[p] & (1 << *i)) != 0)
                .flat_map(|i| solve(b.set(p, 1 << i)))
                .collect()
        }
    }
}

fn main() {
    let b = read_board();

    println!("Solving:\n{}", b);

    let solutions = solve(b);

    println!("Found {} solutions:", solutions.len());

    for i in solutions {
        println!("{} {}", i, check(&i));
    }
}