rustoku_lib/core/
solution.rs

1use crate::core::TechniqueFlags;
2
3use super::board::Board;
4
5/// Solved board and its solution path.
6///
7/// Most of the time, users just want to see the solved board, but this struct also
8/// provides the sequence of moves that led to the solution, which can be useful for debugging
9/// or understanding the solving process.
10#[derive(Debug, Clone)]
11pub struct Solution {
12    /// The solved Sudoku board, represented as a 2D array.
13    pub board: Board,
14    /// The sequence of moves (row, col, value) made to reach the solution.
15    pub solve_path: SolvePath,
16}
17
18/// Solve path associated with a solution.
19#[derive(Debug, Clone, Default)]
20pub struct SolvePath {
21    /// The sequence of steps taken to solve the Sudoku puzzle.
22    pub steps: Vec<SolveStep>,
23}
24
25impl SolvePath {
26    pub fn new() -> Self {
27        SolvePath { steps: Vec::new() }
28    }
29}
30
31/// Single step in the solving process.
32#[derive(Debug, Clone)]
33pub enum SolveStep {
34    /// A placement of a single value on the Sudoku board.
35    Placement {
36        /// The row where the value is placed.
37        row: usize,
38        /// The column where the value is placed.
39        col: usize,
40        /// The value being placed in the Sudoku board.
41        value: u8,
42        /// Flags indicating the technique used for this placement.
43        flags: TechniqueFlags,
44    },
45    /// A removal of a candidate value from the Sudoku board.
46    CandidateElimination {
47        /// The row where the candidate is eliminated.
48        row: usize,
49        /// The column where the candidate is eliminated.
50        col: usize,
51        /// The value being eliminated as a candidate.
52        value: u8,
53        /// Flags indicating the technique used for this elimination.
54        flags: TechniqueFlags,
55    },
56}
57
58impl SolveStep {
59    /// 4-letter code for the solve step.
60    pub fn code(&self) -> &str {
61        match self {
62            Self::CandidateElimination { .. } => "elim",
63            Self::Placement { .. } => "plac",
64        }
65    }
66}