Skip to content

Commit

Permalink
Add Y-Wing solver
Browse files Browse the repository at this point in the history
  • Loading branch information
dharkness committed Jun 7, 2023
1 parent d16b85c commit 43ce1a9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/layout/cells/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl Cell {
PEERS[self.usize()]
}

pub const fn sees(&self, other: Cell) -> bool {
PEERS[self.usize()].has(other)
}

pub const fn label(&self) -> &'static str {
label_from_index(self.0)
}
Expand Down
1 change: 1 addition & 0 deletions src/puzzle/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ pub enum Strategy {
Jellyfish,

SinglesChain,
YWing,
}
1 change: 1 addition & 0 deletions src/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod hidden_tuples;
pub mod intersection_removals;
pub mod naked_tuples;
pub mod singles_chains;
pub mod y_wings;

use crate::layout::*;
use crate::puzzle::*;
Expand Down
53 changes: 53 additions & 0 deletions src/solvers/y_wings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use super::*;

pub fn find_y_wings(board: &Board) -> Option<Effects> {
let mut effects = Effects::new();

let bi_values = board.cells_with_n_candidates(2);
let mut log = false;

println!("bi_values: {}", bi_values);

bi_values.iter().for_each(|cell| {
let (k1, k2) = board.candidates(cell).as_pair().unwrap();
let peers = cell.peers() & bi_values;
if peers.size() < 2 {
return;
}

log = cell.label() == "A2";

let k1_peers = peers & board.candidate_cells(k1);
let k2_peers = peers & board.candidate_cells(k2);

if log {
println!("{}: {}-{}: {}-{}", cell, k1, k2, k1_peers, k2_peers)
}

k1_peers.iter().for_each(|c1| {
let k1_other = board.candidates(c1) - k1;
k2_peers.iter().for_each(|c2| {
let k2_other = board.candidates(c2) - k2;
if k1_other != k2_other || c1.sees(c2) {
return;
}

let k = k1_other.iter().next().unwrap();
let erase = c1.peers() & c2.peers() & board.candidate_cells(k);
if erase.is_empty() {
return;
}

let mut action = Action::new(Strategy::YWing);
action.erase_cells(erase, k);
effects.add_action(action);
});
});
});

if effects.has_actions() {
Some(effects)
} else {
None
}
}

0 comments on commit 43ce1a9

Please sign in to comment.