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)]
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
31impl Default for SolvePath {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37/// Single step in the solving process.
38#[derive(Debug, Clone)]
39pub enum SolveStep {
40 /// A placement of a single value on the Sudoku board.
41 Placement {
42 /// The row where the value is placed.
43 row: usize,
44 /// The column where the value is placed.
45 col: usize,
46 /// The value being placed in the Sudoku board.
47 value: u8,
48 /// Flags indicating the technique used for this placement.
49 flags: TechniqueFlags,
50 },
51 /// A removal of a candidate value from the Sudoku board.
52 CandidateElimination {
53 /// The row where the candidate is eliminated.
54 row: usize,
55 /// The column where the candidate is eliminated.
56 col: usize,
57 /// The value being eliminated as a candidate.
58 value: u8,
59 /// Flags indicating the technique used for this elimination.
60 flags: TechniqueFlags,
61 },
62}
63
64impl SolveStep {
65 /// 4-letter code for the solve step.
66 pub fn code(&self) -> &str {
67 match self {
68 Self::CandidateElimination { .. } => "elim",
69 Self::Placement { .. } => "plac",
70 }
71 }
72}