removed global update
This commit is contained in:
parent
3e3245a977
commit
f9dda499e2
|
@ -57,10 +57,6 @@ impl Cell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_state(&self) -> Option<usize> {
|
|
||||||
self.state
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_allowed(&self) -> Vec<usize> {
|
pub fn get_allowed(&self) -> Vec<usize> {
|
||||||
self.blocking_states
|
self.blocking_states
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -72,23 +72,15 @@ impl Solver {
|
||||||
pub fn solve(&mut self, solver_limit: Option<usize>) -> Result<(), WaveError> {
|
pub fn solve(&mut self, solver_limit: Option<usize>) -> Result<(), WaveError> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
self.display(ui::DisplayMode::Full);
|
self.display(ui::DisplayMode::Full);
|
||||||
let mut number_cell_init: usize = 0;
|
|
||||||
for row in &self.grid {
|
|
||||||
for cell in row {
|
|
||||||
if !cell.is_none() {
|
|
||||||
number_cell_init += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut propagation_counter: usize = 0;
|
let mut propagation_counter: usize = 0;
|
||||||
let mut collapse_counter: usize = 0;
|
let mut collapse_counter: usize = 0;
|
||||||
println!("# started");
|
println!("# started");
|
||||||
self.debug("--------");
|
self.debug("--------");
|
||||||
self.update_possibilities()?;
|
|
||||||
|
|
||||||
while number_cell_init + self.history.len() < self.size * self.size {
|
while self.history.len() < self.size * self.size {
|
||||||
self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len()));
|
self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len()));
|
||||||
while self.last_move_index < self.history.len() && number_cell_init + self.history.len() < self.size * self.size {
|
while self.last_move_index < self.history.len() && self.history.len() < self.size * self.size {
|
||||||
let mut backtrack = 0;
|
let mut backtrack = 0;
|
||||||
match self.propagate_collapse() {
|
match self.propagate_collapse() {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
|
@ -119,7 +111,7 @@ impl Solver {
|
||||||
collapse_counter += 1;
|
collapse_counter += 1;
|
||||||
|
|
||||||
if !self.debug_display && !self.grid_display {
|
if !self.debug_display && !self.grid_display {
|
||||||
self.progress_bar(number_cell_init);
|
self.progress_bar();
|
||||||
}
|
}
|
||||||
if let Some(limit) = solver_limit {
|
if let Some(limit) = solver_limit {
|
||||||
if collapse_counter >= limit {
|
if collapse_counter >= limit {
|
||||||
|
@ -182,6 +174,10 @@ impl Solver {
|
||||||
fn backtrack(&mut self) -> Result<(), WaveError> {
|
fn backtrack(&mut self) -> Result<(), WaveError> {
|
||||||
let mut fork: Option<Step> = None;
|
let mut fork: Option<Step> = None;
|
||||||
while let Some(step) = self.history.pop() {
|
while let Some(step) = self.history.pop() {
|
||||||
|
if self.last_move_index == 0 {
|
||||||
|
self.debug("there is no last move");
|
||||||
|
return Err(WaveError::NoHistory)
|
||||||
|
}
|
||||||
self.last_move_index -= 1;
|
self.last_move_index -= 1;
|
||||||
self.grid[step.position.0][step.position.1].reset_state();
|
self.grid[step.position.0][step.position.1].reset_state();
|
||||||
|
|
||||||
|
|
|
@ -5,39 +5,6 @@ use super::WaveError;
|
||||||
use super::cell;
|
use super::cell;
|
||||||
|
|
||||||
impl Solver {
|
impl Solver {
|
||||||
pub fn update_possibilities(&mut self) -> Result<(), WaveError> {
|
|
||||||
let mut row_used_states: Vec<Vec<cell::BlockingCell>> = vec![vec![]; self.size];
|
|
||||||
let mut column_used_states: Vec<Vec<cell::BlockingCell>> = vec![vec![]; self.size];
|
|
||||||
let mut square_used_states: Vec<Vec<Vec<cell::BlockingCell>>> = vec![vec![vec![]; self.square_size]; self.square_size];
|
|
||||||
|
|
||||||
for row_index in 0..self.size {
|
|
||||||
for column_index in 0..self.size {
|
|
||||||
let Some(state) = self.grid[row_index][column_index].get_state() else {
|
|
||||||
continue
|
|
||||||
};
|
|
||||||
let blocking_cell = cell::BlockingCell {
|
|
||||||
state,
|
|
||||||
position: (row_index, column_index),
|
|
||||||
};
|
|
||||||
row_used_states[row_index].push(blocking_cell.clone());
|
|
||||||
column_used_states[column_index].push(blocking_cell.clone());
|
|
||||||
square_used_states[row_index/self.square_size][column_index/self.square_size].push(blocking_cell);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for row_index in 0..self.size {
|
|
||||||
for column_index in 0..self.size {
|
|
||||||
self.remove_allowed((row_index, column_index), &row_used_states[row_index]
|
|
||||||
.iter()
|
|
||||||
.chain(&column_used_states[column_index])
|
|
||||||
.chain(&square_used_states[row_index / self.square_size][column_index / self.square_size])
|
|
||||||
.cloned()
|
|
||||||
.collect())?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn propagate_collapse(&mut self) -> Result<(), WaveError> {
|
pub fn propagate_collapse(&mut self) -> Result<(), WaveError> {
|
||||||
if self.last_move_index >= self.history.len() {
|
if self.last_move_index >= self.history.len() {
|
||||||
self.debug(&format!("x nothing to propagate"));
|
self.debug(&format!("x nothing to propagate"));
|
||||||
|
|
|
@ -2,6 +2,7 @@ use text_io::read;
|
||||||
|
|
||||||
use super::Solver;
|
use super::Solver;
|
||||||
use super::cell;
|
use super::cell;
|
||||||
|
use super::Step;
|
||||||
|
|
||||||
|
|
||||||
const CHAR_SET: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
const CHAR_SET: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
@ -80,6 +81,11 @@ impl Solver {
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
if value <= self.size {
|
if value <= self.size {
|
||||||
self.grid[row_index][column_index].collapse(&cell::CollapseOption::Set(value))?;
|
self.grid[row_index][column_index].collapse(&cell::CollapseOption::Set(value))?;
|
||||||
|
self.history.push(Step {
|
||||||
|
position: (row_index, column_index),
|
||||||
|
state_set: value,
|
||||||
|
num_allowed_states: 1,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
|
@ -95,9 +101,9 @@ impl Solver {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn progress_bar(&self, number_cell_init: usize) {
|
pub fn progress_bar(&self) {
|
||||||
let bar_size = (self.size + self.square_size - 1)*2 + 1;
|
let bar_size = (self.size + self.square_size - 1)*2 + 1;
|
||||||
let progress = self.history.len()*bar_size/(self.size*self.size - number_cell_init);
|
let progress = self.history.len()*bar_size/(self.size*self.size);
|
||||||
let to_do = bar_size - progress;
|
let to_do = bar_size - progress;
|
||||||
print!("\r[{}{}]", "#".repeat(progress), "-".repeat(to_do));
|
print!("\r[{}{}]", "#".repeat(progress), "-".repeat(to_do));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue