From 2bdfd8cc6c0339b7eeb8617121d4ee6ce43e1522 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sun, 14 Jan 2024 01:25:45 +0100 Subject: [PATCH] Add cost function. --- examples/brute_force.rs | 2 +- src/belt_finding/brute_force.rs | 26 +++++++++++++++++++++++--- src/belt_finding/mod.rs | 4 ++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/brute_force.rs b/examples/brute_force.rs index ef7a883..69aabba 100644 --- a/examples/brute_force.rs +++ b/examples/brute_force.rs @@ -47,7 +47,7 @@ fn main() { match args.mode { Mode::Solutions => { while b.next_finish_state() { - println!("{}\n{}\n{}", b.count(), b.solution_count(), b); + println!("{}\n{}\n{}\n{}", b.count(), b.solution_count(), b.cost(), b); } println!("Solutions: {}\nStates: {}", b.solution_count(), b.count()); diff --git a/src/belt_finding/brute_force.rs b/src/belt_finding/brute_force.rs index d57fd67..338695f 100644 --- a/src/belt_finding/brute_force.rs +++ b/src/belt_finding/brute_force.rs @@ -13,7 +13,7 @@ pub struct BruteforceField { underground_horizontal: bool, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Copy)] pub enum PathField { Belt { pos: Position, @@ -221,7 +221,7 @@ impl Bruteforce { fn modify_path_field(&mut self, path_field: PathField) { let i = self.modify_pointer(); - let last = self.problems[i].path.last().unwrap().clone(); + let last = *self.problems[i].path.last().unwrap(); match (last, &path_field) { (PathField::Belt { pos: _, dir: _ }, PathField::Belt { pos: _, dir: _ }) => {} @@ -400,7 +400,7 @@ impl Bruteforce { // } fn modify_remove(&mut self) -> bool { - if let Some([second_last, last]) = self.modify_path().last_chunk().cloned() { + if let Some([second_last, last]) = self.modify_path().last_chunk().copied() { match last { PathField::Belt { pos, dir } => { if second_last.dir() == &dir { @@ -515,6 +515,26 @@ impl Bruteforce { false } + pub fn get_paths(&self) -> Vec> { + self.problems.iter().map(|p| p.path.clone()).collect() + } + + pub fn cost(&self) -> f64 { + self.problems + .iter() + .flat_map(|p| { + p.path.iter().map(|f| match f { + PathField::Belt { pos: _, dir: _ } => 1.5, + PathField::Underground { + pos: _, + dir: _, + len: _, + } => 17.5, + }) + }) + .sum() + } + fn pos_in_direction(&self, pos: &Position, dir: &Direction, len: usize) -> Option { match dir { Direction::Up => pos.y.checked_sub(len).map(|y| Position::new(pos.x, y)), diff --git a/src/belt_finding/mod.rs b/src/belt_finding/mod.rs index 1f90724..0880e2c 100644 --- a/src/belt_finding/mod.rs +++ b/src/belt_finding/mod.rs @@ -332,8 +332,8 @@ impl Problem { dbg!(c); - let xoffset = 1; - let yoffset = 1; + let xoffset = 2; + let yoffset = 2; if let Some((cx, cy)) = c { *conflicts.get_mut(cx, cy) = 1;