Add cost function.

This commit is contained in:
hal8174 2024-01-14 01:25:45 +01:00
parent 02e65f6fe3
commit 2bdfd8cc6c
3 changed files with 26 additions and 6 deletions

View file

@ -47,7 +47,7 @@ fn main() {
match args.mode { match args.mode {
Mode::Solutions => { Mode::Solutions => {
while b.next_finish_state() { 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()); println!("Solutions: {}\nStates: {}", b.solution_count(), b.count());

View file

@ -13,7 +13,7 @@ pub struct BruteforceField {
underground_horizontal: bool, underground_horizontal: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Copy)]
pub enum PathField { pub enum PathField {
Belt { Belt {
pos: Position, pos: Position,
@ -221,7 +221,7 @@ impl Bruteforce {
fn modify_path_field(&mut self, path_field: PathField) { fn modify_path_field(&mut self, path_field: PathField) {
let i = self.modify_pointer(); 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) { match (last, &path_field) {
(PathField::Belt { pos: _, dir: _ }, PathField::Belt { pos: _, dir: _ }) => {} (PathField::Belt { pos: _, dir: _ }, PathField::Belt { pos: _, dir: _ }) => {}
@ -400,7 +400,7 @@ impl Bruteforce {
// } // }
fn modify_remove(&mut self) -> bool { 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 { match last {
PathField::Belt { pos, dir } => { PathField::Belt { pos, dir } => {
if second_last.dir() == &dir { if second_last.dir() == &dir {
@ -515,6 +515,26 @@ impl Bruteforce {
false false
} }
pub fn get_paths(&self) -> Vec<Vec<PathField>> {
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<Position> { fn pos_in_direction(&self, pos: &Position, dir: &Direction, len: usize) -> Option<Position> {
match dir { match dir {
Direction::Up => pos.y.checked_sub(len).map(|y| Position::new(pos.x, y)), Direction::Up => pos.y.checked_sub(len).map(|y| Position::new(pos.x, y)),

View file

@ -332,8 +332,8 @@ impl Problem {
dbg!(c); dbg!(c);
let xoffset = 1; let xoffset = 2;
let yoffset = 1; let yoffset = 2;
if let Some((cx, cy)) = c { if let Some((cx, cy)) = c {
*conflicts.get_mut(cx, cy) = 1; *conflicts.get_mut(cx, cy) = 1;