Performance improvements and fix warnings

This commit is contained in:
hal8174 2024-01-02 23:22:20 +01:00
parent 68d0f64058
commit b5dcbccf02
6 changed files with 202 additions and 55 deletions

View file

@ -27,16 +27,16 @@ pub enum PathField {
}
impl PathField {
fn pos(&self) -> &Position {
match self {
PathField::Belt { pos, dir: _ } => pos,
PathField::Underground {
pos,
dir: _,
len: _,
} => pos,
}
}
// fn pos(&self) -> &Position {
// match self {
// PathField::Belt { pos, dir: _ } => pos,
// PathField::Underground {
// pos,
// dir: _,
// len: _,
// } => pos,
// }
// }
fn dir(&self) -> &Direction {
match self {
@ -408,7 +408,11 @@ impl Bruteforce {
pos,
dir: second_last.dir().clockwise(),
});
return true;
if self.is_next_free(&pos, &second_last.dir().clockwise()) {
return true;
} else {
return self.modify_remove();
}
}
if second_last.dir().clockwise() == dir {
@ -416,7 +420,11 @@ impl Bruteforce {
pos,
dir: second_last.dir().counter_clockwise(),
});
return true;
if self.is_next_free(&pos, &second_last.dir().counter_clockwise()) {
return true;
} else {
return self.modify_remove();
}
}
if second_last.dir().counter_clockwise() == dir
@ -452,6 +460,13 @@ impl Bruteforce {
// &mut self.problems[i].path
// }
fn is_next_free(&self, pos: &Position, dir: &Direction) -> bool {
let i = self.modify_pointer();
self.pos_in_direction(pos, dir, 1)
.filter(|p| !self.map.get(p.x, p.y).blocked || self.problems[i].end_pos == *p)
.is_some()
}
// Add an Path elemeent
fn add(&mut self) -> bool {
let (pos, dir) = self.path_field_end(self.add_path().last().unwrap());
@ -459,7 +474,7 @@ impl Bruteforce {
if let Some(p) = self.pos_in_direction(&pos, &dir, 1) {
if !self.map.get(p.x, p.y).blocked {
self.apply_path_field(PathField::Belt { pos: p, dir });
return true;
return self.is_next_free(&p, &dir);
}
}
@ -599,3 +614,114 @@ impl Display for Bruteforce {
Ok(())
}
}
#[cfg(test)]
mod test {
use super::problems;
#[test]
fn simple() {
let mut b = problems::simple();
while b.next_finish_state() {}
assert_eq!(b.solution_count(), 9);
}
#[test]
fn snake() {
let mut b = problems::snake();
while b.next_finish_state() {}
assert_eq!(b.solution_count(), 5);
}
}
pub mod problems {
use super::*;
pub fn simple() -> Bruteforce {
let mut b = BruteforceBuilder::new(6, 8);
b.add_path(
(Position::new(1, 0), Direction::Down),
(Position::new(0, 0), Direction::Up),
);
b.add_path(
(Position::new(4, 0), Direction::Down),
(Position::new(1, 7), Direction::Up),
);
b.set_blocked_range(0, 2, 5, 5, true);
b.build()
}
pub fn snake() -> Bruteforce {
let mut p = BruteforceBuilder::new(14, 3);
p.add_path(
(Position::new(0, 0), Direction::Right),
(Position::new(13, 0), Direction::Right),
);
p.add_path(
(Position::new(0, 1), Direction::Right),
(Position::new(13, 1), Direction::Right),
);
p.add_path(
(Position::new(0, 2), Direction::Right),
(Position::new(13, 2), Direction::Right),
);
p.set_blocked_range(3, 2, 10, 2, true);
p.build()
}
pub fn weaving() -> Bruteforce {
let mut b = BruteforceBuilder::new(14, 6);
b.add_path(
(Position::new(0, 0), Direction::Right),
(Position::new(13, 0), Direction::Right),
);
b.add_path(
(Position::new(0, 1), Direction::Right),
(Position::new(13, 1), Direction::Right),
);
b.add_path(
(Position::new(0, 2), Direction::Right),
(Position::new(13, 2), Direction::Right),
);
b.add_path(
(Position::new(0, 3), Direction::Right),
(Position::new(13, 3), Direction::Right),
);
b.add_path(
(Position::new(0, 4), Direction::Right),
(Position::new(13, 4), Direction::Right),
);
b.add_path(
(Position::new(0, 5), Direction::Right),
(Position::new(13, 5), Direction::Right),
);
// b.set_blocked_range(7, 2, 10, 2, true);
// b.set_blocked_range(7, 3, 10, 4, true);
b.set_blocked_range(3, 2, 10, 3, true);
b.set_blocked(2, 0, true);
b.set_blocked(11, 0, true);
b.set_blocked(2, 5, true);
b.set_blocked(11, 5, true);
b.build()
}
}