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
pub type SudokuMatrix = [[u8; 9]; 9];
type Coord = [usize; 2];

#[derive(Debug)]
pub struct Sudoku {
    matrix: SudokuMatrix,
    pub available: [[bool; 9]; 9],
}

impl Sudoku {
    // fn new(matrix: SudokuMatrix) -> Self {
    //     Self {
    //         matrix,
    //         coords: Self::find_empty_coords(&matrix),
    //     }
    // }

    fn find_availability(sudoku: &SudokuMatrix) -> [[bool; 9]; 9] {
        let mut available = [[false; 9]; 9];
        for i in 0..9 {
            for j in 0..9 {
                available[i][j] = sudoku[i][j] == 0;
            }
        }
        available
    }

    /// Convert from a `[[u8; 9]; 9]` matrix (array-of-array) to a `Sudoku`
    pub fn from_matrix(matrix: SudokuMatrix) -> Self {
        matrix.into()
    }

    pub fn finished(&self) -> bool {
        for i in 0..9 {
            for j in 0..9 {
                if self[[i, j]] == 0 {
                    return false;
                }
            }
        }
        true
    }

    // /// Check whether the sudoku is still valid
    pub fn conflict(&self, v: u8, coord: Coord) -> Option<[usize; 2]> {
        if let Some(coord) = self.conflict_row(v, coord) {
            Some(coord)
        } else if let Some(coord) = self.conflict_col(v, coord) {
            Some(coord)
        } else if let Some(coord) = self.conflict_box(v, coord) {
            Some(coord)
        } else {
            None
        }
    }

    fn conflict_row(&self, v: u8, coord: Coord) -> Option<[usize; 2]> {
        let [i, _] = coord;
        for j in 0..9 {
            if self[[i, j]] == v {
                return Some([i, j]);
            }
        }
        None
    }
    fn conflict_col(&self, v: u8, coord: Coord) -> Option<[usize; 2]> {
        let [_, j] = coord;
        for i in 0..9 {
            if self[[i, j]] == v {
                return Some([i, j]);
            }
        }
        None
    }
    fn conflict_box(&self, v: u8, coord: Coord) -> Option<[usize; 2]> {
        let [i_, j_] = coord;
        let [i_, j_] = [i_ / 3, j_ / 3];
        for i in 3 * i_..3 * i_ + 3 {
            // "inner" i and j; indexes of individual cells
            for j in 3 * j_..3 * j_ + 3 {
                if v == self[[i, j]] {
                    return Some([i, j]);
                }
            }
        }
        None
    }
}

impl std::convert::From<SudokuMatrix> for Sudoku {
    fn from(matrix: SudokuMatrix) -> Self {
        Self {
            matrix,
            available: Self::find_availability(&matrix),
        }
    }
}

impl std::ops::Index<Coord> for Sudoku {
    type Output = u8;

    fn index(&self, coords: Coord) -> &Self::Output {
        &self.matrix[coords[0]][coords[1]]
    }
}

impl std::ops::IndexMut<Coord> for Sudoku {
    fn index_mut(&mut self, coords: Coord) -> &mut u8 {
        &mut self.matrix[coords[0]][coords[1]]
    }
}

use std::fmt;

impl fmt::Display for Sudoku {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = String::with_capacity(180);
        for i in 0..9 {
            for j in 0..9 {
                s.push_str(&format!("{} ", self[[i, j]]))
            }
            s.push('\n')
        }
        write!(f, "{}", s)
    }
}