Changed bruteforce end pos.

This commit is contained in:
hal8174 2024-02-18 14:46:57 +01:00
parent 7fdf32342a
commit 2bf648f657
2 changed files with 95 additions and 49 deletions

View file

@ -40,6 +40,22 @@ impl BruteforceBuilder {
self.map.get_mut(x, y).blocked = blocked;
}
pub fn set_underground(&mut self, x: usize, y: usize, dir: Option<Direction>) {
match dir {
Some(d) => {
if d.vertical() {
self.map.get_mut(x, y).underground_vertical = true;
} else {
self.map.get_mut(x, y).underground_horizontal = true;
}
}
None => {
self.map.get_mut(x, y).underground_vertical = false;
self.map.get_mut(x, y).underground_horizontal = false;
}
}
}
pub fn set_blocked_range(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, blocked: bool) {
for x in x1..=x2 {
for y in y1..=y2 {
@ -65,10 +81,10 @@ impl BruteforceBuilder {
};
for [start, end] in self.path {
b.map
.get_mut(start.0.x as usize, start.0.y as usize)
.blocked = true;
b.map.get_mut(end.0.x as usize, end.0.y as usize).blocked = true;
// b.map
// .get_mut(start.0.x as usize, start.0.y as usize)
// .blocked = true;
// b.map.get_mut(end.0.x as usize, end.0.y as usize).blocked = true;
b.problems.push(Problem {
path: vec![PathField::Belt {
pos: start.0,
@ -299,20 +315,22 @@ impl Bruteforce {
}
fn check_finish(&self, i: usize) -> bool {
match self.problems[i].path.last().unwrap() {
PathField::Belt { pos, dir } => Some(pos.in_direction(dir, 1))
.filter(|p| {
p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse()
})
.is_some(),
PathField::Underground { pos, dir, len } => {
Some(pos.in_direction(dir, *len as PositionType + 1))
.filter(|p| {
p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse()
})
.is_some()
}
}
self.problems[i].path.last().unwrap().end_pos()
== (self.problems[i].end_pos, self.problems[i].end_dir)
// match self.problems[i].path.last().unwrap() {
// PathField::Belt { pos, dir } => Some(pos.in_direction(dir, 1))
// .filter(|p| {
// p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse()
// })
// .is_some(),
// PathField::Underground { pos, dir, len } => {
// Some(pos.in_direction(dir, *len as PositionType + 1))
// .filter(|p| {
// p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse()
// })
// .is_some()
// }
// }
}
fn modify_underground(&mut self, pos: &Position, dir: &Direction, len: u8) -> bool {
@ -492,18 +510,21 @@ impl Bruteforce {
fn is_next_free(&self, pos: &Position, dir: &Direction) -> bool {
let i = self.modify_pointer();
pos.in_direction(dir, 1)
.in_range(
&Position::new(0, 0),
&Position::new(
self.map.width as PositionType,
self.map.height as PositionType,
),
)
.filter(|&p| {
!self.map.get(p.x as usize, p.y as usize).blocked || self.problems[i].end_pos == *p
})
.is_some()
self.check_finish(i)
|| pos
.in_direction(dir, 1)
.in_range(
&Position::new(0, 0),
&Position::new(
self.map.width as PositionType,
self.map.height as PositionType,
),
)
.filter(|&p| {
!self.map.get(p.x as usize, p.y as usize).blocked
|| self.problems[i].end_pos == *p
})
.is_some()
}
// Add an Path elemeent
@ -686,7 +707,7 @@ mod test {
};
}
test_bruteforce!(simple 9; mid 238240; snake 5);
test_bruteforce!(simple 9; mid 308986; snake 5);
}
pub mod problems {
@ -700,11 +721,15 @@ pub mod problems {
(Position::new(0, 0), Direction::Up),
);
b.set_blocked(1, 0, true);
b.add_path(
(Position::new(4, 0), Direction::Down),
(Position::new(1, 7), Direction::Up),
);
b.set_blocked(4, 0, true);
b.set_blocked_range(0, 2, 5, 5, true);
b.build()
@ -717,29 +742,31 @@ pub mod problems {
(Position::new(0, 0), Direction::Down),
(Position::new(5, 5), Direction::Down),
);
b.set_blocked(0, 0, true);
b.add_path(
(Position::new(0, 5), Direction::Up),
(Position::new(5, 0), Direction::Up),
);
b.set_blocked(0, 0, true);
b.build()
}
pub fn snake() -> Bruteforce {
let mut p = BruteforceBuilder::new(14, 3);
let mut p = BruteforceBuilder::new(13, 3);
p.add_path(
(Position::new(0, 0), Direction::Right),
(Position::new(13, 0), Direction::Right),
(Position::new(12, 0), Direction::Right),
);
p.add_path(
(Position::new(0, 1), Direction::Right),
(Position::new(13, 1), Direction::Right),
(Position::new(12, 1), Direction::Right),
);
p.add_path(
(Position::new(0, 2), Direction::Right),
(Position::new(13, 2), Direction::Right),
(Position::new(12, 2), Direction::Right),
);
p.set_blocked_range(3, 2, 10, 2, true);