sudokugen 0.3.0

Sudokugen is a sudoku solving and generating library
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use super::indexed_map::{Indexed, IndexedMap, Map};
use crate::board::{Board, CellLoc};
use std::fmt;
use std::{
    collections::{BTreeSet, HashMap},
    hash::Hash,
};

#[derive(Hash, Debug, PartialEq, Eq, Copy, Clone)]
pub enum Block {
    Line(usize),
    Col(usize),
    Square(usize),
}

impl Block {
    fn with_value(&self, value: u8) -> (Self, u8) {
        (*self, value)
    }
}

impl CellLoc {
    fn get_blocks_(&self) -> [Block; 3] {
        [
            Block::Line(self.line()),
            Block::Col(self.col()),
            Block::Square(self.square()),
        ]
    }
}

impl Indexed for CellLoc {
    fn idx(&self) -> usize {
        self.get_index()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct NoCadidatesLeftError(CellLoc);

impl fmt::Display for NoCadidatesLeftError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "No candidates left for this cell {}", self.0)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct UndoSetValue {
    moves: Vec<(u8, CellLoc, Block)>,
    options: (CellLoc, Option<BTreeSet<u8>>),
    affected_cell_options: Vec<(CellLoc, u8)>,
}

impl UndoSetValue {
    pub fn alternative_options(&self) -> &Option<BTreeSet<u8>> {
        &self.options.1
    }
}

pub struct Candidates<'a> {
    pub value: &'a u8,
    pub block: &'a Block,
    pub cells: &'a BTreeSet<CellLoc>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CandidateCache {
    possible_values: IndexedMap<CellLoc, BTreeSet<u8>>,
    candidate_cells: HashMap<(Block, u8), BTreeSet<CellLoc>>,
}

impl CandidateCache {
    pub fn from_board(board: &Board) -> Self {
        let possible_values = Self::calculate_possible_values(board);

        let mut candidate_cache = CandidateCache {
            possible_values,
            candidate_cells: HashMap::with_capacity(board.board_size().get_base_size().pow(4) * 3),
        };

        for cell in candidate_cache.possible_values.keys() {
            let possible_values = candidate_cache.possible_values.get(cell);

            for value in 1..=(board.board_size().get_base_size() as u8).pow(2) {
                if let Some(possible_values) = possible_values {
                    if possible_values.contains(&value) {
                        for block in &cell.get_blocks_() {
                            candidate_cache
                                .candidate_cells
                                .entry(block.with_value(value))
                                .or_default()
                                .insert(*cell);
                        }
                    }
                }
            }
        }

        candidate_cache
    }

    fn calculate_possible_values(board: &Board) -> IndexedMap<CellLoc, BTreeSet<u8>> {
        let mut possible_values = IndexedMap::new(board.board_size().get_base_size().pow(4));
        for cell in board.iter_cells() {
            if let Some(values) = cell.get_possible_values(board) {
                possible_values.insert(cell, values);
            }
        }
        possible_values
    }

    pub fn set_value(
        &mut self,
        value: u8,
        cell: CellLoc,
    ) -> Result<UndoSetValue, NoCadidatesLeftError> {
        // remove all possible values for this cell
        let maybe_options = self.possible_values.remove(&cell);
        let mut moves = Vec::new();

        // in this line, column and square this value is no longer relevant so it's removed from cache
        for block in &cell.get_blocks_() {
            let candidates = self.candidate_cells.remove(&block.with_value(value));

            if let Some(candidates) = candidates {
                moves.extend(
                    &mut candidates
                        .iter()
                        .map(|candidate| (value, *candidate, *block)),
                );
            }

            // remove the cell as candidate for all other values in this line, col and square
            if let Some(other_values) = &maybe_options {
                for other_value in other_values {
                    if *other_value != value {
                        if let Some(candidates) = self
                            .candidate_cells
                            .get_mut(&block.with_value(*other_value))
                        {
                            if candidates.remove(&cell) {
                                moves.push((*other_value, cell, *block));
                            }
                        }
                    }
                }
            }
        }

        let mut affected_cell_options = Vec::new();

        let affected_cells = cell
            .iter_line()
            .chain(cell.iter_col())
            .chain(cell.iter_square());

        for affected_cell in affected_cells {
            if let Some(values) = self.possible_values.get_mut(&affected_cell) {
                assert!(!values.is_empty());

                if values.remove(&value) {
                    affected_cell_options.push((affected_cell, value));

                    // for every cell affected by this one (same line, col and square)
                    // that cell is no longer a candidate for this value in all it's blocks
                    for block in &affected_cell.get_blocks_() {
                        if let Some(cells) = self.candidate_cells.get_mut(&block.with_value(value))
                        {
                            if cells.remove(&affected_cell) {
                                moves.push((value, affected_cell, *block));
                            }
                        }
                    }
                }

                if values.is_empty() {
                    self.undo(UndoSetValue {
                        moves,
                        options: (cell, maybe_options),
                        affected_cell_options,
                    });
                    return Err(NoCadidatesLeftError(cell));
                }
            }
        }

        Ok(UndoSetValue {
            moves,
            options: (cell, maybe_options),
            affected_cell_options,
        })
    }

    pub fn reset_candidates(
        &mut self,
        cell: &CellLoc,
        options: BTreeSet<u8>,
    ) -> Option<BTreeSet<u8>> {
        for value in &options {
            self.add_candidate(value, cell);
        }

        self.possible_values.insert(*cell, options)
    }

    fn add_candidate(&mut self, value: &u8, cell: &CellLoc) {
        for block in &cell.get_blocks_() {
            self.candidate_cells
                .entry(block.with_value(*value))
                .or_default()
                .insert(*cell);
        }
    }

    pub fn remove_candidate(&mut self, value: &u8, cell: &CellLoc) {
        // first remove the value as an option for that cell
        if let Some(options) = self.possible_values.get_mut(cell) {
            if options.remove(value) {
                // if value was an option for that cell then also remove the cell as
                // a candidate for that value in all blocks
                for block in &cell.get_blocks_() {
                    if let Some(cells) = self.candidate_cells.get_mut(&block.with_value(*value)) {
                        cells.remove(cell);
                    }
                }
            }
        }
    }

    pub fn undo(&mut self, undo: UndoSetValue) {
        if let Some(options) = undo.options.1 {
            let cell = undo.options.0;
            self.possible_values.insert(cell, options);
        }

        for (cell, value) in undo.affected_cell_options {
            self.possible_values.entry(cell).or_default().insert(value);
        }

        for (value, cell, block) in undo.moves {
            self.candidate_cells
                .entry(block.with_value(value))
                .or_default()
                .insert(cell);
        }
    }

    pub fn iter_candidates(&self) -> impl Iterator<Item = Candidates> {
        self.candidate_cells
            .iter()
            .map(|((block, value), cells)| Candidates {
                value,
                block,
                cells,
            })
    }

    pub fn possible_values(&self) -> &IndexedMap<CellLoc, BTreeSet<u8>> {
        &self.possible_values
    }

    #[cfg(test)]
    fn candidates_at(&self, block: &Block, value: &u8) -> Option<&BTreeSet<CellLoc>> {
        self.candidate_cells.get(&block.with_value(*value))
    }

    #[cfg(debug)]
    pub fn possible_values_from_candidates(&self) -> HashMap<CellLoc, BTreeSet<u8>> {
        let mut possible_values: HashMap<CellLoc, BTreeSet<u8>> = HashMap::new();

        for (ValueBlock { value, .. }, cells) in &self.candidate_cells {
            for cell in cells {
                possible_values.entry(*cell).or_default().insert(*value);
            }
        }

        possible_values
    }
}

#[cfg(test)]
mod tests {
    use super::Block::{Col, Line, Square};
    use super::CandidateCache;
    use crate::{
        board::{Board, BoardSize, CellLoc},
        solver::indexed_map::Map,
    };
    use std::collections::BTreeSet;

    fn candidate_cache_from_board(board: &Board) -> CandidateCache {
        CandidateCache::from_board(board)
    }

    fn candidate_cache_from_board_str(board_str: &str) -> CandidateCache {
        candidate_cache_from_board(&(*board_str).parse().unwrap())
    }

    #[test]
    fn test_iter_candidates() {
        let cc = candidate_cache_from_board(&Board::new(BoardSize::NineByNine));

        assert_eq!(cc.iter_candidates().count(), 81 * 3);
        assert_eq!(
            cc.iter_candidates()
                .map(|candidate| *candidate.value)
                .collect::<BTreeSet<u8>>(),
            (1..=9).collect()
        );
    }

    #[test]
    fn possible_values_after_parse() {
        let board =
            "...4..87.4.3......2....3..9..62....7...9.6...3.9.8...........4.8725........72.6.."
                .parse()
                .unwrap();
        let cc = candidate_cache_from_board(&board);
        for cell in board.iter_cells() {
            if board.get(&cell).is_some() {
                assert_eq!(cc.possible_values().get(&cell), None);
            }
        }
    }

    #[test]
    fn possible_locs_after_parse() {
        let cc = candidate_cache_from_board_str(
            "
        1234567..
        456......
        78.......
        2........
        3........
        5........
        6........
        .........
        .........
        ",
        );

        assert_eq!(
            cc.candidates_at(&Line(0), &9),
            Some(
                &vec![
                    CellLoc::at(0, 7, BoardSize::NineByNine),
                    CellLoc::at(0, 8, BoardSize::NineByNine),
                ]
                .drain(..)
                .collect()
            )
        );

        assert_eq!(
            cc.candidates_at(&Col(0), &9),
            Some(
                &vec![
                    CellLoc::at(7, 0, BoardSize::NineByNine),
                    CellLoc::at(8, 0, BoardSize::NineByNine),
                ]
                .into_iter()
                .collect()
            )
        );

        assert_eq!(
            cc.candidates_at(&Square(0), &9),
            Some(
                &vec![CellLoc::at(2, 2, BoardSize::NineByNine)]
                    .drain(..)
                    .collect()
            )
        );
    }

    #[test]
    fn set_value_updates_possible_values() {
        let board = "
        12..
        ....
        ....
        ....
        "
        .parse()
        .unwrap();

        let mut cc = candidate_cache_from_board(&board);

        cc.set_value(3, CellLoc::at(1, 0, BoardSize::FourByFour))
            .unwrap();

        assert_eq!(
            cc.possible_values().get(&board.cell_at(1, 1)),
            Some(&vec![4_u8].into_iter().collect())
        );
    }

    #[test]
    fn set_value_updates_possible_loc() {
        let board: Board = "
        12..
        ....
        ....
        ....
        "
        .parse()
        .unwrap();

        let mut cc = candidate_cache_from_board(&board);

        cc.set_value(3, board.cell_at(3, 0)).unwrap();

        // square already contains 3 therefore it's removed from possible locations
        assert_eq!(cc.candidates_at(&Square(2), &3), None);

        // line already contains 3 therefore it's removed from possible locations
        assert_eq!(cc.candidates_at(&Line(3), &3), None);

        // column already contains 3 therefore it's removed from possible locations
        assert_eq!(cc.candidates_at(&Col(0), &3), None);

        // setting the 3 above removes the other possible location for a 3 in square 0
        assert_eq!(
            cc.candidates_at(&Square(0), &3),
            Some(&vec![board.cell_at(1, 1)].into_iter().collect()),
        );

        // setting a cell removes it as a possibility to all other values in its blocks
        assert!(!cc
            .candidates_at(&Square(2), &4)
            .unwrap()
            .contains(&board.cell_at(3, 0)));
    }

    #[test]
    fn test_set_value_removes_cell_as_candidate_for_other_values() {
        let board: Board = "
        ....
        ....
        ....
        ....
        "
        .parse()
        .unwrap();

        let mut cc = candidate_cache_from_board(&board);

        assert!(cc
            .candidates_at(&Line(0), &1)
            .unwrap()
            .contains(&board.cell_at(0, 0)));

        cc.set_value(2, board.cell_at(0, 0)).unwrap();

        assert!(!cc
            .candidates_at(&Line(0), &1)
            .unwrap()
            .contains(&board.cell_at(0, 0)));
    }

    #[test]
    fn test_undo() {
        let board: Board = "
        ....
        ....
        ....
        ....
        "
        .parse()
        .unwrap();

        let cc = candidate_cache_from_board(&board);
        let mut cc_clone = cc.clone();

        let undo = cc_clone.set_value(2, board.cell_at(3, 2)).unwrap();
        cc_clone.undo(undo);

        assert_eq!(cc, cc_clone);
    }
}