diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | 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::<u8>::new(2); + let mut solver = solvers::Solver::<u32>::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<T: num::PrimInt> { solve_stack: Vec<SaveState<T>>, } -#[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<T: num::PrimInt> { + /// Mask of all currently used stones pub bitmask: T, + /// sum of all placed stones pub index: u32, + /// the row index + pub row: u32, +} + +impl<T: num::PrimInt> std::cmp::Ord for SaveState<T> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.index.cmp(&other.index) + } +} +impl<T: num::PrimInt> std::cmp::PartialOrd for SaveState<T> { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.index.cmp(&other.index)) + } +} +impl<T: num::PrimInt> std::cmp::PartialEq for SaveState<T> { + fn eq(&self, other: &Self) -> bool { + self.index.eq(&other.index) + } } impl<T: num::PrimInt> SaveState<T> { @@ -76,6 +100,10 @@ impl<T: num::PrimInt> SaveState<T> { Self { bitmask: T::zero(), index: 0, + row: unsafe { + COUNT += 1; + COUNT + }, } } @@ -85,6 +113,7 @@ impl<T: num::PrimInt> SaveState<T> { } 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<T: num::PrimInt> SaveState<T> { 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<T: num::PrimInt> Solver<T> { } } + //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 } } |