From b8b32592259235ab3c9566ce9c32593c0baf68ba Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sat, 21 Dec 2019 19:47:57 +0100 Subject: Add dummy solver --- src/main.rs | 2 +- src/solvers.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index ddcc7c8..7da7bd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod solvers; fn main() { - let mut solver = solvers::Solver::::new(2); + let mut solver = solvers::Solver::::new(4); let wall = solver.solve(); wall.output(solver.n, solver.h); diff --git a/src/solvers.rs b/src/solvers.rs index 6df4b4a..447f753 100644 --- a/src/solvers.rs +++ b/src/solvers.rs @@ -65,10 +65,34 @@ pub struct Solver { solve_stack: Vec>, } -#[derive(Clone)] +// used during row creation, will get deprecated +static mut COUNT: u32 = 0; + +/// Save the current state for each row +#[derive(Clone, Eq, Copy)] pub struct SaveState { + /// Mask of all currently used stones pub bitmask: T, + /// sum of all placed stones pub index: u32, + /// the row index + pub row: u32, +} + +impl std::cmp::Ord for SaveState { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.index.cmp(&other.index) + } +} +impl std::cmp::PartialOrd for SaveState { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.index.cmp(&other.index)) + } +} +impl std::cmp::PartialEq for SaveState { + fn eq(&self, other: &Self) -> bool { + self.index.eq(&other.index) + } } impl SaveState { @@ -76,6 +100,10 @@ impl SaveState { Self { bitmask: T::zero(), index: 0, + row: unsafe { + COUNT += 1; + COUNT + }, } } @@ -85,6 +113,7 @@ impl SaveState { } pub fn get_bit(&self, stone: u32) -> T { + println!("{},", stone); self.bitmask & T::from(1 << stone).expect("Requested stone index out of bounds") } @@ -92,9 +121,10 @@ impl SaveState { self.get_bit(index - self.index) == T::zero() } - pub fn bridge_gap(&mut self, index: u32) { + pub fn bridge_gap(mut self, index: u32) -> Self { self.set_bit(index - self.index); - self.index = index + self.index = index; + self } } @@ -110,13 +140,25 @@ impl Solver { } } + //dummy solver pub fn solve(&mut self) -> Wall { - let wall = Wall::create_empty(self.w); + let mut wall = Wall::create_empty(self.w); wall.heights .iter() .take(2) .zip(0..1) .for_each(|(x, i)| self.solve_stack[*x as usize].set_bit(i)); + for i in 0..(self.n * self.h) { + let machine = self + .solve_stack + .iter() + .filter(|x| x.bridges_gap(i + 1)) + .min() + .unwrap() + .bridge_gap(i + 1); + wall.heights.push(machine.row); + } + wall } } -- cgit v1.2.3-54-g00ecf