Skip to content

Commit

Permalink
hidden single for row
Browse files Browse the repository at this point in the history
  • Loading branch information
Nervonment committed May 31, 2024
1 parent 94a3fb2 commit 871e700
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 55 deletions.
60 changes: 7 additions & 53 deletions src/bin/example/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,20 @@ use std::io::{self, stdout};

use crossterm::{terminal::EnterAlternateScreen, ExecutableCommand};
use sudoku::{
game::{
generator::*,
hint::{hidden_single, naked_single},
solver::{SudokuSolver, SudokuSolverH},
},
game::generator::random_sudoku_puzzle_normal,
neo::{puzzle::SudokuPuzzle, techniques::hidden_single},
ui::{draw_grid, draw_numbers},
};

fn main() -> io::Result<()> {
// let times = 1000;
// let mut total_invoke_cnt_h = 0;
// let mut total_invoke_cnt_hp = 0;
// for _ in 0..times {
// let puzzle = random_sudoku_puzzle(80);
// let mut solver = SudokuSolverH::new(puzzle);
// let mut solver_hp = SudokuSolverHP::new(puzzle);
// solver.get_solution();
// total_invoke_cnt_h += solver.invoke_cnt;
// solver_hp.get_solution();
// total_invoke_cnt_hp += solver_hp.invoke_cnt;
// }
// println!("{}", total_invoke_cnt_h / times);
// println!("{}", total_invoke_cnt_hp / times);

// let puzzle = [
// [5, 0, 0, 0, 0, 0, 3, 0, 0],
// [0, 2, 0, 1, 0, 0, 0, 7, 0],
// [0, 0, 8, 0, 0, 0, 0, 0, 9],
// [0, 4, 0, 0, 0, 7, 0, 0, 0],
// [0, 0, 0, 8, 2, 1, 0, 0, 0],
// [0, 0, 0, 6, 0, 0, 0, 1, 0],
// [3, 0, 0, 0, 0, 0, 8, 0, 0],
// [0, 6, 0, 0, 0, 4, 0, 2, 0],
// [0, 0, 9, 0, 0, 0, 0, 0, 5],
// ];
// let puzzle = [
// [0, 0, 2, 0, 0, 9, 6, 0, 5],
// [4, 0, 8, 0, 0, 1, 0, 0, 0],
// [0, 0, 0, 0, 2, 0, 0, 0, 0],
// [1, 0, 6, 9, 0, 0, 0, 5, 0],
// [0, 8, 0, 0, 0, 0, 0, 0, 0],
// [0, 0, 0, 0, 5, 7, 0, 9, 0],
// [0, 0, 4, 0, 0, 2, 0, 0, 3],
// [0, 0, 0, 0, 0, 0, 0, 2, 4],
// [0, 0, 0, 4, 1, 8, 7, 0, 0],
// ];
// let puzzle = random_sudoku_puzzle(80);
// let puzzle = random_sudoku_puzzle_1(55, 55, 90);
let puzzle = random_sudoku_puzzle_phishing();
let mut solver = SudokuSolverH::new(puzzle);
solver.have_unique_solution();

let board = random_sudoku_puzzle_normal();
let puzzle = SudokuPuzzle::new(board);
let res = hidden_single(&puzzle);
stdout().execute(EnterAlternateScreen)?;
draw_grid()?;
draw_numbers(&puzzle, &puzzle, &[[true; 9]; 9])?;
draw_numbers(&board, &board, &[[true; 9]; 9])?;
println!("");
println!("");
println!("{}, {}", solver.invoke_cnt, solver.unsure_cnt);
println!("{:?}", hidden_single(&puzzle));
println!("{:?}", naked_single(&puzzle));

println!("{:?}", res);
Ok(())
}
12 changes: 11 additions & 1 deletion src/neo/puzzle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use super::utils::{block_idx_2_coord, coord_2_block};

pub trait Grid {
fn grid_val(&self, r: usize, c: usize) -> i8;
fn is_grid_empty(&self, r: usize, c: usize) -> bool;
}

pub trait TrackingCandidates {
fn is_candidate_of(&self, r: usize, c: usize, num: i8) -> bool;

Expand Down Expand Up @@ -162,8 +167,13 @@ impl SudokuPuzzle {
}
res
}
}

pub fn grid_val(&self, r: usize, c: usize) -> i8 {
impl Grid for SudokuPuzzle {
fn grid_val(&self, r: usize, c: usize) -> i8 {
self.board[r][c]
}
fn is_grid_empty(&self, r: usize, c: usize) -> bool {
self.board[r][c] == 0
}
}
18 changes: 18 additions & 0 deletions src/neo/techniques.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::puzzle::{Grid, TrackingCandidates};

pub fn hidden_single(puzzle: &(impl TrackingCandidates + Grid)) -> Option<(usize, usize, i8)> {
for r in 0..9 {
for num in 1..=9 {
if puzzle.grid_cnt_for_candidate_in_row(r, num) == 1 {
let c = (0..9)
.filter(|c: &usize| puzzle.is_candidate_of(r, *c, num))
.next()
.unwrap();
if puzzle.is_grid_empty(r, c) {
return Some((r, c, num));
}
}
}
}
None
}
2 changes: 1 addition & 1 deletion src/neo/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
};

use super::puzzle::Fillable;
use super::puzzle::{Fillable, Grid};

#[test]
fn sudoku_puzzle() {
Expand Down

0 comments on commit 871e700

Please sign in to comment.