Skip to content

Commit

Permalink
Use const function to generate neighbors for all cells
Browse files Browse the repository at this point in the history
  • Loading branch information
dharkness committed May 27, 2023
1 parent ddbb40e commit ccb4759
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 167 deletions.
1 change: 1 addition & 0 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::solvers::intersection_removals::find_intersection_removals;
const FILLED: &str = "|---------=========---------=========---------=========---------=========---------|";
const EMPTY : &str = "| |";

/// Generates a full board.
pub struct Generator {
rng: ThreadRng,
cells: Vec<Cell>,
Expand Down
2 changes: 1 addition & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod houses;
mod knowns;

pub use board::Board;
pub use cells::{Bit, Bits, Cell, generate_code_for_neighbors, Set as CellSet};
pub use cells::{Bit, Bits, Cell, Set as CellSet};
pub use houses::{Coord, House, Shape};
pub use knowns::{Known, Set as KnownSet};
9 changes: 5 additions & 4 deletions src/layout/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt;

use super::{Cell, CellSet, House, Known, KnownSet};

/// Tracks the full state of a puzzle in play.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Board {
givens: CellSet,
Expand Down Expand Up @@ -53,21 +54,21 @@ impl Board {
}

pub const fn candidates(&self, cell: Cell) -> KnownSet {
self.candidates[cell.index() as usize]
self.candidates[cell.usize()]
}

pub fn is_candidate(&self, cell: Cell, known: Known) -> bool {
self.candidates[cell.index() as usize][known]
self.candidates[cell.usize()][known]
}

pub fn remove_candidate(&mut self, cell: Cell, known: Known) {
let set = &mut self.candidates[cell.index() as usize];
let set = &mut self.candidates[cell.usize()];
*set -= known;
self.valid = !set.is_empty();
}

pub const fn value(&self, cell: Cell) -> u8 {
self.values[cell.index() as usize]
self.values[cell.usize()]
}

pub fn set_given(&mut self, cell: Cell, known: Known) {
Expand Down
2 changes: 0 additions & 2 deletions src/layout/cells.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
///! Provides Cell and Set to track collections of cells and methods to manipulate them.
mod bit;
mod cell;
mod generate;
mod label;
mod set;

pub use bit::Bit;
pub use cell::Cell;
pub use generate::generate_code_for_neighbors;
pub use set::{Bits, Set};
2 changes: 1 addition & 1 deletion src/layout/cells/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::layout::cells::label::index_from_label;

use super::Cell;

/// Specifies a single cell by its bit in a bit field.
/// Specifies a single cell by its position in a bit field.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Bit(u128);

Expand Down
127 changes: 36 additions & 91 deletions src/layout/cells/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ impl Cell {
}

pub const fn house(&self, shape: Shape) -> House {
HOUSES[self.usize()][shape as usize]
HOUSES[self.usize()][shape.usize()]
}

pub const fn row(&self) -> House {
HOUSES[self.usize()][Shape::Row as usize]
HOUSES[self.usize()][Shape::Row.usize()]
}

pub const fn column(&self) -> House {
HOUSES[self.usize()][Shape::Column as usize]
HOUSES[self.usize()][Shape::Column.usize()]
}

pub const fn block(&self) -> House {
HOUSES[self.usize()][Shape::Block as usize]
HOUSES[self.usize()][Shape::Block.usize()]
}

pub const fn coord_in_row(&self) -> Coord {
HOUSES_COORDS[self.usize()][Shape::Row as usize]
HOUSES_COORDS[self.usize()][Shape::Row.usize()]
}

pub const fn coord_in_column(&self) -> Coord {
HOUSES_COORDS[self.usize()][Shape::Column as usize]
HOUSES_COORDS[self.usize()][Shape::Column.usize()]
}

pub const fn coord_in_block(&self) -> Coord {
HOUSES_COORDS[self.usize()][Shape::Block as usize]
HOUSES_COORDS[self.usize()][Shape::Block.usize()]
}

pub const fn neighbors(&self) -> Set {
Expand All @@ -66,6 +66,20 @@ impl Cell {
}
}

impl From<i32> for Cell {
fn from(index: i32) -> Self {
debug_assert!(index >= 0 && index < Cell::COUNT as i32);
Cell::new(index as u32)
}
}

impl From<usize> for Cell {
fn from(index: usize) -> Self {
debug_assert!(index < Cell::COUNT as usize);
Cell::new(index as u32)
}
}

impl From<&str> for Cell {
fn from(label: &str) -> Self {
Self(index_from_label(label))
Expand Down Expand Up @@ -121,90 +135,21 @@ const HOUSES_COORDS: [[Coord; 3]; 81] = {

/// Holds the neighbors for every unique cell.
/// A cell's neighbors are all the cells in the same row, column and block, excluding the cell itself.
/// TODO Use a function to generate this.
const NEIGHBORS: [Set; 81] = [
Set::new(0b000000001000000001000000001000000001000000001000000001000000111000000111111111110),
Set::new(0b000000010000000010000000010000000010000000010000000010000000111000000111111111101),
Set::new(0b000000100000000100000000100000000100000000100000000100000000111000000111111111011),
Set::new(0b000001000000001000000001000000001000000001000000001000000111000000111000111110111),
Set::new(0b000010000000010000000010000000010000000010000000010000000111000000111000111101111),
Set::new(0b000100000000100000000100000000100000000100000000100000000111000000111000111011111),
Set::new(0b001000000001000000001000000001000000001000000001000000111000000111000000110111111),
Set::new(0b010000000010000000010000000010000000010000000010000000111000000111000000101111111),
Set::new(0b100000000100000000100000000100000000100000000100000000111000000111000000011111111),
Set::new(0b000000001000000001000000001000000001000000001000000001000000111111111110000000111),
Set::new(0b000000010000000010000000010000000010000000010000000010000000111111111101000000111),
Set::new(0b000000100000000100000000100000000100000000100000000100000000111111111011000000111),
Set::new(0b000001000000001000000001000000001000000001000000001000000111000111110111000111000),
Set::new(0b000010000000010000000010000000010000000010000000010000000111000111101111000111000),
Set::new(0b000100000000100000000100000000100000000100000000100000000111000111011111000111000),
Set::new(0b001000000001000000001000000001000000001000000001000000111000000110111111111000000),
Set::new(0b010000000010000000010000000010000000010000000010000000111000000101111111111000000),
Set::new(0b100000000100000000100000000100000000100000000100000000111000000011111111111000000),
Set::new(0b000000001000000001000000001000000001000000001000000001111111110000000111000000111),
Set::new(0b000000010000000010000000010000000010000000010000000010111111101000000111000000111),
Set::new(0b000000100000000100000000100000000100000000100000000100111111011000000111000000111),
Set::new(0b000001000000001000000001000000001000000001000000001000111110111000111000000111000),
Set::new(0b000010000000010000000010000000010000000010000000010000111101111000111000000111000),
Set::new(0b000100000000100000000100000000100000000100000000100000111011111000111000000111000),
Set::new(0b001000000001000000001000000001000000001000000001000000110111111111000000111000000),
Set::new(0b010000000010000000010000000010000000010000000010000000101111111111000000111000000),
Set::new(0b100000000100000000100000000100000000100000000100000000011111111111000000111000000),
Set::new(0b000000001000000001000000001000000111000000111111111110000000001000000001000000001),
Set::new(0b000000010000000010000000010000000111000000111111111101000000010000000010000000010),
Set::new(0b000000100000000100000000100000000111000000111111111011000000100000000100000000100),
Set::new(0b000001000000001000000001000000111000000111000111110111000001000000001000000001000),
Set::new(0b000010000000010000000010000000111000000111000111101111000010000000010000000010000),
Set::new(0b000100000000100000000100000000111000000111000111011111000100000000100000000100000),
Set::new(0b001000000001000000001000000111000000111000000110111111001000000001000000001000000),
Set::new(0b010000000010000000010000000111000000111000000101111111010000000010000000010000000),
Set::new(0b100000000100000000100000000111000000111000000011111111100000000100000000100000000),
Set::new(0b000000001000000001000000001000000111111111110000000111000000001000000001000000001),
Set::new(0b000000010000000010000000010000000111111111101000000111000000010000000010000000010),
Set::new(0b000000100000000100000000100000000111111111011000000111000000100000000100000000100),
Set::new(0b000001000000001000000001000000111000111110111000111000000001000000001000000001000),
Set::new(0b000010000000010000000010000000111000111101111000111000000010000000010000000010000),
Set::new(0b000100000000100000000100000000111000111011111000111000000100000000100000000100000),
Set::new(0b001000000001000000001000000111000000110111111111000000001000000001000000001000000),
Set::new(0b010000000010000000010000000111000000101111111111000000010000000010000000010000000),
Set::new(0b100000000100000000100000000111000000011111111111000000100000000100000000100000000),
Set::new(0b000000001000000001000000001111111110000000111000000111000000001000000001000000001),
Set::new(0b000000010000000010000000010111111101000000111000000111000000010000000010000000010),
Set::new(0b000000100000000100000000100111111011000000111000000111000000100000000100000000100),
Set::new(0b000001000000001000000001000111110111000111000000111000000001000000001000000001000),
Set::new(0b000010000000010000000010000111101111000111000000111000000010000000010000000010000),
Set::new(0b000100000000100000000100000111011111000111000000111000000100000000100000000100000),
Set::new(0b001000000001000000001000000110111111111000000111000000001000000001000000001000000),
Set::new(0b010000000010000000010000000101111111111000000111000000010000000010000000010000000),
Set::new(0b100000000100000000100000000011111111111000000111000000100000000100000000100000000),
Set::new(0b000000111000000111111111110000000001000000001000000001000000001000000001000000001),
Set::new(0b000000111000000111111111101000000010000000010000000010000000010000000010000000010),
Set::new(0b000000111000000111111111011000000100000000100000000100000000100000000100000000100),
Set::new(0b000111000000111000111110111000001000000001000000001000000001000000001000000001000),
Set::new(0b000111000000111000111101111000010000000010000000010000000010000000010000000010000),
Set::new(0b000111000000111000111011111000100000000100000000100000000100000000100000000100000),
Set::new(0b111000000111000000110111111001000000001000000001000000001000000001000000001000000),
Set::new(0b111000000111000000101111111010000000010000000010000000010000000010000000010000000),
Set::new(0b111000000111000000011111111100000000100000000100000000100000000100000000100000000),
Set::new(0b000000111111111110000000111000000001000000001000000001000000001000000001000000001),
Set::new(0b000000111111111101000000111000000010000000010000000010000000010000000010000000010),
Set::new(0b000000111111111011000000111000000100000000100000000100000000100000000100000000100),
Set::new(0b000111000111110111000111000000001000000001000000001000000001000000001000000001000),
Set::new(0b000111000111101111000111000000010000000010000000010000000010000000010000000010000),
Set::new(0b000111000111011111000111000000100000000100000000100000000100000000100000000100000),
Set::new(0b111000000110111111111000000001000000001000000001000000001000000001000000001000000),
Set::new(0b111000000101111111111000000010000000010000000010000000010000000010000000010000000),
Set::new(0b111000000011111111111000000100000000100000000100000000100000000100000000100000000),
Set::new(0b111111110000000111000000111000000001000000001000000001000000001000000001000000001),
Set::new(0b111111101000000111000000111000000010000000010000000010000000010000000010000000010),
Set::new(0b111111011000000111000000111000000100000000100000000100000000100000000100000000100),
Set::new(0b111110111000111000000111000000001000000001000000001000000001000000001000000001000),
Set::new(0b111101111000111000000111000000010000000010000000010000000010000000010000000010000),
Set::new(0b111011111000111000000111000000100000000100000000100000000100000000100000000100000),
Set::new(0b110111111111000000111000000001000000001000000001000000001000000001000000001000000),
Set::new(0b101111111111000000111000000010000000010000000010000000010000000010000000010000000),
Set::new(0b011111111111000000111000000100000000100000000100000000100000000100000000100000000),
];
const NEIGHBORS: [Set; 81] = {
let mut sets: [Set; 81] = [Set::empty(); 81];
let mut i = 0;

while i < 81 {
let cell = Cell::new(i as u32);
sets[i] = Set::empty()
.union(cell.row().cells())
.union(cell.column().cells())
.union(cell.block().cells())
.without(cell);
i += 1;
}
sets
};

#[cfg(test)]
mod tests {
Expand Down
17 changes: 0 additions & 17 deletions src/layout/cells/generate.rs

This file was deleted.

Loading

0 comments on commit ccb4759

Please sign in to comment.