Skip to content

Commit

Permalink
Merge pull request #1 from dharkness/add-xy-chains-solver
Browse files Browse the repository at this point in the history
Add XY-Chain solver
  • Loading branch information
dharkness committed Sep 4, 2023
2 parents 1e77f23 + 58674b1 commit 78f5bcc
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 4 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Sudoku Rust

Sudoku generator in Rust as a learning project.
Sudoku generator, solver and player console application in Rust.

[x] Learn Rust
[x] Have fun
[x] Generate new puzzles for my TypeScript Sudoku app

## How to run

To run the application,:

1. Install Rust
2. Clone the repository
3. Run the application

```bash
cargo run
```

## Usage

Once running, you'll be presented with the available commands.
6 changes: 4 additions & 2 deletions src/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::symbols::UNKNOWN_VALUE;

const URL: &str = "https://www.sudokuwiki.org/sudoku.htm?bd=";

const SOLVERS: [Solver; 18] = [
const SOLVERS: [Solver; 19] = [
crate::solvers::intersection_removals::find_intersection_removals,
crate::solvers::naked_tuples::find_naked_pairs,
crate::solvers::naked_tuples::find_naked_triples,
Expand All @@ -27,10 +27,11 @@ const SOLVERS: [Solver; 18] = [
crate::solvers::xyz_wings::find_xyz_wings,
crate::solvers::bugs::find_bugs,
crate::solvers::avoidable_rectangles::find_avoidable_rectangles,
crate::solvers::xy_chains::find_xy_chains,
crate::solvers::unique_rectangles::find_unique_rectangles,
crate::solvers::empty_rectangles::find_empty_rectangles,
];
const SOLVER_LABELS: [&str; 18] = [
const SOLVER_LABELS: [&str; 19] = [
"intersection removal",
"naked pair",
"naked triple",
Expand All @@ -47,6 +48,7 @@ const SOLVER_LABELS: [&str; 18] = [
"xyz-wing",
"bug",
"avoidable rectangle",
"xy-chain",
"unique rectangle",
"empty rectangle",
];
Expand Down
19 changes: 19 additions & 0 deletions src/puzzle/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ impl Action {
}
}

pub fn new_erase_cells(strategy: Strategy, cells: CellSet, known: Known) -> Self {
Self {
strategy,
set: HashMap::new(),
erase: cells
.iter()
.map(|cell| (cell, KnownSet::empty() + known))
.collect(),
}
}

pub fn new_erase_knowns(strategy: Strategy, cell: Cell, knowns: KnownSet) -> Self {
Self {
strategy,
set: HashMap::new(),
erase: HashMap::from([(cell, knowns)]),
}
}

pub fn is_empty(&self) -> bool {
self.set.is_empty() && self.erase.is_empty()
}
Expand Down
8 changes: 8 additions & 0 deletions src/puzzle/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ impl Effects {
self.add_action(Action::new_erase(strategy, cell, known));
}

pub fn add_erase_cells(&mut self, strategy: Strategy, cells: CellSet, known: Known) {
self.add_action(Action::new_erase_cells(strategy, cells, known));
}

pub fn add_erase_knowns(&mut self, strategy: Strategy, cell: Cell, knowns: KnownSet) {
self.add_action(Action::new_erase_knowns(strategy, cell, knowns));
}

pub fn erases(&self, cell: Cell, known: Known) -> bool {
self.actions.iter().any(|action| action.erases(cell, known))
}
Expand Down
4 changes: 3 additions & 1 deletion src/puzzle/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum Strategy {
YWing, // (Known, pivot Cell, arms (Cell, Cell))
XYZWing, // (Known, pivot Cell, arms (Cell, Cell))

EmptyRectangle, // (Known, Block, Row, Column, Cell) - CellSet instead of three houses
XYChain, // (Known, Vec<Cell>)
UniqueRectangle, // (Cell, Cell, Cell, Cell)

EmptyRectangle, // (Known, Block, Row, Column, Cell) - CellSet instead of three houses
}
1 change: 1 addition & 0 deletions src/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod naked_tuples;
pub mod singles_chains;
pub mod skyscrapers;
pub mod unique_rectangles;
pub mod xy_chains;
pub mod xyz_wings;
pub mod y_wings;

Expand Down
Loading

0 comments on commit 78f5bcc

Please sign in to comment.