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
//! A [Sudoku puzzle](https://en.wikipedia.org/wiki/Sudoku) is a
//! `n^2` x `n^2` array with sub-arrays of size `n` x `n`. Each row, column, and
//! sub-array contains the values `1` through `n` with no repeats.

use super::{
    latin_square::{self, LatinSquare},
    ExactCover,
};
use core::iter;
use std::collections::HashSet;

/// An instance of a Sudoku puzzle.
#[derive(Debug)]
pub struct Sudoku {
    /// The list of possible values and positions that are valid for this Sudoku
    /// puzzle.
    possibilities: Vec<Possibility>,
    /// The list of constraints that must be satisfied for this Sudoku puzzle.
    constraints: Vec<Constraint>,
}

impl Sudoku {
    /// Create a new new Sudoku puzzle.
    ///
    /// The puzzle has size n^2 x n^2 (where `n = box_side_length`) and the
    /// given list of filled values.
    pub fn new(
        box_side_length: usize,
        filled_values: impl IntoIterator<Item = latin_square::Possibility>,
    ) -> Self {
        let side_length = box_side_length * box_side_length;
        let filled_values: Vec<_> = filled_values.into_iter().collect();

        let latin = latin_square::LatinSquare::new(side_length, filled_values.iter().copied());

        let satisfied: HashSet<_> = filled_values
            .iter()
            .copied()
            .map(|latin_poss| Possibility::from_latin(latin_poss, box_side_length))
            .flat_map(Possibility::satisfied_constraints)
            .collect();

        let possibilities = latin
            .possibilities
            .into_iter()
            .map(|latin_poss| Possibility::from_latin(latin_poss, box_side_length))
            .collect();

        let constraints = latin
            .constraints
            .into_iter()
            .map(Constraint::from)
            .chain(Constraint::all_square_number(box_side_length))
            .filter(|cons| !satisfied.contains(cons))
            .collect();

        Self {
            possibilities,
            constraints,
        }
    }
}

impl ExactCover for Sudoku {
    type Constraint = Constraint;
    type Possibility = Possibility;

    fn satisfies(poss: &Self::Possibility, cons: &Self::Constraint) -> bool {
        use Constraint::*;

        match cons {
            Latin(latin_cons) => LatinSquare::satisfies(&Possibility::into(*poss), latin_cons),
            SquareNumber { square, value } => poss.square == *square && poss.value == *value,
        }
    }

    fn is_optional(_cons: &Self::Constraint) -> bool {
        false
    }
}

/// A position and value for a box inside of a Sudoku puzzle.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Possibility {
    /// The row position of the box.
    ///
    /// The values ranges from 0 to `n - 1`, where `n` is the length of the
    /// Sudoku board.
    pub row: usize,

    /// The column position of the box.
    ///
    /// The values ranges from 0 to `n - 1`, where `n` is the length of the
    /// Sudoku board.
    pub column: usize,

    /// The index of the subgrid.
    ///
    /// The values ranges from 0 to `n - 1`, where `n` is the length of the
    /// Sudoku board. This field is redundant in identifying where the box is
    /// inside of the Sudoku board, however it is necessary to speed up checking
    /// which `Constraint`s are satisfied by this `Possibility`.
    pub square: usize,

    /// The value present inside of the box.
    ///
    /// The values ranges from 1 to `n`, where `n` is the length of the
    /// Sudoku board.
    pub value: usize,
}

impl Possibility {
    /// Convert a `latin_square::Possibility` to a `sudoku::Possibility`.
    pub fn from_latin(latin: latin_square::Possibility, box_side_length: usize) -> Self {
        let side_length = box_side_length * box_side_length;
        let index = latin.row * side_length + latin.column;
        let square = ((index % side_length) / box_side_length)
            + box_side_length * (index / (side_length * box_side_length));

        Possibility {
            row: latin.row,
            column: latin.column,
            value: latin.value,
            square,
        }
    }

    /// Return an iterator over the `Constraint`s that are satisfied by this
    /// `Possibility`.
    pub fn satisfied_constraints(self) -> impl Iterator<Item = Constraint> {
        iter::successors(
            Some(Constraint::Latin(latin_square::Constraint::RowNumber {
                row: self.row,
                value: self.value,
            })),
            move |cons| match cons {
                Constraint::Latin(latin_square::Constraint::RowNumber { .. }) => {
                    Some(Constraint::Latin(latin_square::Constraint::ColumnNumber {
                        column: self.column,
                        value: self.value,
                    }))
                }
                Constraint::Latin(latin_square::Constraint::ColumnNumber { .. }) => {
                    Some(Constraint::Latin(latin_square::Constraint::RowColumn {
                        row: self.row,
                        column: self.column,
                    }))
                }
                Constraint::Latin(latin_square::Constraint::RowColumn { .. }) => {
                    Some(Constraint::SquareNumber {
                        square: self.square,
                        value: self.value,
                    })
                }
                Constraint::SquareNumber { .. } => None,
            },
        )
    }
}

impl Into<latin_square::Possibility> for Possibility {
    fn into(self) -> latin_square::Possibility {
        latin_square::Possibility {
            row: self.row,
            column: self.column,
            value: self.value,
        }
    }
}

/// A condition which must be satisfied in order to solve a Sudoku puzzle.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Constraint {
    /// A constraint which is also shared by a Latin Square puzzle.
    Latin(latin_square::Constraint),
    /// A condition that each square (or sub-grid) should only have a single
    /// instance of a numeric value.
    SquareNumber {
        /// The square index.
        square: usize,
        /// The unique numeric value
        value: usize,
    },
}

impl Constraint {
    fn all_square_number(box_side_length: usize) -> impl Iterator<Item = Constraint> {
        let side_length = box_side_length * box_side_length;

        crate::util::two_combination_iter([side_length, side_length + 1], [0, 1])
            .map(|[square, value]| Constraint::SquareNumber { square, value })
    }
}

impl From<latin_square::Constraint> for Constraint {
    fn from(src: latin_square::Constraint) -> Self {
        Constraint::Latin(src)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn p(row: usize, column: usize, square: usize, value: usize) -> Possibility {
        Possibility {
            row,
            column,
            square,
            value,
        }
    }

    fn c_row(row: usize, value: usize) -> Constraint {
        Constraint::Latin(latin_square::Constraint::RowNumber { row, value })
    }

    fn c_col(column: usize, value: usize) -> Constraint {
        Constraint::Latin(latin_square::Constraint::ColumnNumber { column, value })
    }

    fn c_row_col(row: usize, column: usize) -> Constraint {
        Constraint::Latin(latin_square::Constraint::RowColumn { row, column })
    }

    fn c_square(square: usize, value: usize) -> Constraint {
        Constraint::SquareNumber { square, value }
    }

    #[test]
    fn check_generated_possibilities_constraints() {
        let mut sudoku = Sudoku::new(
            2,
            vec![
                // top row
                latin_square::tests::p(0, 0, 1),
                latin_square::tests::p(0, 1, 2),
                latin_square::tests::p(0, 2, 3),
                latin_square::tests::p(0, 3, 4),
                // middle bits
                latin_square::tests::p(1, 0, 3),
                latin_square::tests::p(2, 0, 2),
                latin_square::tests::p(1, 3, 2),
                latin_square::tests::p(2, 3, 3),
                // bottom row
                latin_square::tests::p(3, 0, 4),
                latin_square::tests::p(3, 1, 3),
                latin_square::tests::p(3, 2, 2),
                latin_square::tests::p(3, 3, 1),
            ],
        );

        sudoku.possibilities.sort();
        assert_eq!(
            sudoku.possibilities,
            vec![
                p(1, 1, 0, 1),
                p(1, 1, 0, 2),
                p(1, 1, 0, 3),
                p(1, 1, 0, 4),
                p(1, 2, 1, 1),
                p(1, 2, 1, 2),
                p(1, 2, 1, 3),
                p(1, 2, 1, 4),
                p(2, 1, 2, 1),
                p(2, 1, 2, 2),
                p(2, 1, 2, 3),
                p(2, 1, 2, 4),
                p(2, 2, 3, 1),
                p(2, 2, 3, 2),
                p(2, 2, 3, 3),
                p(2, 2, 3, 4),
            ]
        );
        sudoku.constraints.sort();
        assert_eq!(
            sudoku.constraints,
            vec![
                c_row(1, 1),
                c_row(1, 4),
                c_row(2, 1),
                c_row(2, 4),
                c_col(1, 1),
                c_col(1, 4),
                c_col(2, 1),
                c_col(2, 4),
                c_row_col(1, 1),
                c_row_col(1, 2),
                c_row_col(2, 1),
                c_row_col(2, 2),
                c_square(0, 4),
                c_square(1, 1),
                c_square(2, 1),
                c_square(3, 4),
            ]
        );
    }

    #[test]
    fn solve_small_sudoku() {
        let sudoku = Sudoku::new(
            2,
            vec![
                // top row
                latin_square::tests::p(0, 0, 1),
                latin_square::tests::p(0, 1, 2),
                latin_square::tests::p(0, 2, 3),
                latin_square::tests::p(0, 3, 4),
                // middle bits
                latin_square::tests::p(1, 0, 3),
                latin_square::tests::p(2, 0, 2),
                latin_square::tests::p(1, 3, 2),
                latin_square::tests::p(2, 3, 3),
                // bottom row
                latin_square::tests::p(3, 0, 4),
                latin_square::tests::p(3, 1, 3),
                latin_square::tests::p(3, 2, 2),
                latin_square::tests::p(3, 3, 1),
            ],
        );

        let mut solver =
            crate::solver::Solver::<Sudoku>::new(&sudoku.possibilities, &sudoku.constraints);
        let solutions = solver.all_solutions();

        assert_eq!(solutions.len(), 1);
        assert_eq!(
            solutions[0],
            vec![
                &p(1, 1, 0, 4),
                &p(1, 2, 1, 1),
                &p(2, 1, 2, 1),
                &p(2, 2, 3, 4)
            ]
        );
    }
}