Skip to content

Commit

Permalink
Add Almost Unique Rectangle solver
Browse files Browse the repository at this point in the history
  • Loading branch information
dharkness committed Nov 27, 2023
1 parent 5d8bc58 commit 2114adf
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 53 deletions.
5 changes: 3 additions & 2 deletions src/commands/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn solve_puzzles(args: SolveArgs) {
let mut count = 0;
let mut solved = 0;

println!(" µs NS HS NP NT NQ HP HT HQ PP PT BL XW SC YW ER SF XZ JF SK TS AR XY UR FW EU HU WZ BG");
println!(" µs NS HS NP NT NQ HP HT HQ PP PT BL XW SC YW ER SF XZ JF SK TS AR XY UR AU FW EU HU WZ BG");
for puzzle in stdin.lock().lines().map_while(Result::ok) {
if cancelable.is_canceled() {
break;
Expand Down Expand Up @@ -250,7 +250,7 @@ impl CSVReporter {

fn format_counts(&self, counts: &HashMap<Strategy, i32>) -> String {
format!(
"{:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2}",
"{:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2} {:>2}",
// counts.get(&Strategy::Peer).unwrap_or(0),
counts.get(&Strategy::NakedSingle).unwrap_or(&0),
counts.get(&Strategy::HiddenSingle).unwrap_or(&0),
Expand Down Expand Up @@ -278,6 +278,7 @@ impl CSVReporter {
counts.get(&Strategy::TwoStringKite).unwrap_or(&0),
counts.get(&Strategy::XYChain).unwrap_or(&0),
counts.get(&Strategy::UniqueRectangle).unwrap_or(&0),
counts.get(&Strategy::AlmostUniqueRectangle).unwrap_or(&0),
counts.get(&Strategy::Fireworks).unwrap_or(&0),
counts.get(&Strategy::ExtendedUniqueRectangle).unwrap_or(&0),
counts.get(&Strategy::HiddenUniqueRectangle).unwrap_or(&0),
Expand Down
24 changes: 22 additions & 2 deletions src/puzzle/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ impl Board {
self.knowns
}

/// Returns the set of all cells solved with the known, including givens.
pub const fn known_cells(&self, known: Known) -> CellSet {
self.solved_cells_by_known[known.usize()]
}

/// Returns an iterator of all known cells with their digit, including givens.
pub fn known_iter(&self) -> impl Iterator<Item = (Cell, Known)> + '_ {
self.knowns
Expand Down Expand Up @@ -178,6 +183,16 @@ impl Board {
self.givens
}

/// Returns the set of all cells given the known.
pub fn given_cells(&self, known: Known) -> CellSet {
self.givens & self.solved_cells_by_known[known.usize()]
}

/// Returns true if the cell could not have been solved by the known due to a peer with the given.
pub fn blocked_by_given(&self, cell: Cell, known: Known) -> bool {
!(cell.peers() & self.givens & self.solved_cells_by_known[known.usize()]).is_empty()
}

/// Returns true if every cell on the board has a digit.
pub const fn is_fully_solved(&self) -> bool {
self.knowns.is_full()
Expand All @@ -194,8 +209,13 @@ impl Board {
}

/// Returns the set of all solved cells, excluding givens.
pub const fn solved(&self) -> CellSet {
self.knowns.minus(self.givens)
pub fn solved(&self) -> CellSet {
self.knowns - self.givens
}

/// Returns the set of all cells solved with the known, excluding givens.
pub fn solved_cells(&self, known: Known) -> CellSet {
self.solved_cells_by_known[known.usize()] - self.givens
}

/// Returns true if every cell in the house has a digit.
Expand Down
3 changes: 3 additions & 0 deletions src/puzzle/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum Strategy {

XYChain, // (Known, Vec<Cell>)
UniqueRectangle, // (KnownSet, Cell, Cell, Cell, Cell)
AlmostUniqueRectangle, // (KnownSet, Cell, Cell, Cell, Cell)
Fireworks, // (KnownSet, Cell, Cell, Cell)
ExtendedUniqueRectangle, // (KnownSet, Cell, Cell, Cell, Cell, Cell, Cell)
HiddenUniqueRectangle, // (KnownSet, Cell, Cell, Cell, Cell)
Expand Down Expand Up @@ -141,6 +142,7 @@ impl Strategy {
Self::Skyscraper => Difficulty::Diabolical,
Self::XYChain => Difficulty::Diabolical,
Self::UniqueRectangle => Difficulty::Diabolical,
Self::AlmostUniqueRectangle => Difficulty::Diabolical,
Self::Fireworks => Difficulty::Diabolical,
Self::ExtendedUniqueRectangle => Difficulty::Diabolical,
Self::HiddenUniqueRectangle => Difficulty::Diabolical,
Expand Down Expand Up @@ -181,6 +183,7 @@ impl Strategy {
Self::WXYZWing => "WXYZ-Wing",
Self::XYChain => "XY-Chain",
Self::UniqueRectangle => "Unique Rectangle",
Self::AlmostUniqueRectangle => "Almost Unique Rectangle",
Self::Fireworks => "Fireworks",
Self::ExtendedUniqueRectangle => "Extended Unique Rectangle",
Self::HiddenUniqueRectangle => "Hidden Unique Rectangle",
Expand Down
Loading

0 comments on commit 2114adf

Please sign in to comment.