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; 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) { pub fn set_blocked_range(&mut self, x1: usize, y1: usize, x2: usize, y2: usize, blocked: bool) {
for x in x1..=x2 { for x in x1..=x2 {
for y in y1..=y2 { for y in y1..=y2 {
@ -65,10 +81,10 @@ impl BruteforceBuilder {
}; };
for [start, end] in self.path { for [start, end] in self.path {
b.map // b.map
.get_mut(start.0.x as usize, start.0.y as usize) // .get_mut(start.0.x as usize, start.0.y as usize)
.blocked = true; // .blocked = true;
b.map.get_mut(end.0.x as usize, end.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 { b.problems.push(Problem {
path: vec![PathField::Belt { path: vec![PathField::Belt {
pos: start.0, pos: start.0,
@ -299,20 +315,22 @@ impl Bruteforce {
} }
fn check_finish(&self, i: usize) -> bool { fn check_finish(&self, i: usize) -> bool {
match self.problems[i].path.last().unwrap() { self.problems[i].path.last().unwrap().end_pos()
PathField::Belt { pos, dir } => Some(pos.in_direction(dir, 1)) == (self.problems[i].end_pos, self.problems[i].end_dir)
.filter(|p| { // match self.problems[i].path.last().unwrap() {
p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse() // PathField::Belt { pos, dir } => Some(pos.in_direction(dir, 1))
}) // .filter(|p| {
.is_some(), // p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse()
PathField::Underground { pos, dir, len } => { // })
Some(pos.in_direction(dir, *len as PositionType + 1)) // .is_some(),
.filter(|p| { // PathField::Underground { pos, dir, len } => {
p == &self.problems[i].end_pos && dir != &self.problems[i].end_dir.reverse() // Some(pos.in_direction(dir, *len as PositionType + 1))
}) // .filter(|p| {
.is_some() // 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 { 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 { fn is_next_free(&self, pos: &Position, dir: &Direction) -> bool {
let i = self.modify_pointer(); let i = self.modify_pointer();
pos.in_direction(dir, 1) self.check_finish(i)
.in_range( || pos
&Position::new(0, 0), .in_direction(dir, 1)
&Position::new( .in_range(
self.map.width as PositionType, &Position::new(0, 0),
self.map.height as PositionType, &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 )
}) .filter(|&p| {
.is_some() !self.map.get(p.x as usize, p.y as usize).blocked
|| self.problems[i].end_pos == *p
})
.is_some()
} }
// Add an Path elemeent // 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 { pub mod problems {
@ -700,11 +721,15 @@ pub mod problems {
(Position::new(0, 0), Direction::Up), (Position::new(0, 0), Direction::Up),
); );
b.set_blocked(1, 0, true);
b.add_path( b.add_path(
(Position::new(4, 0), Direction::Down), (Position::new(4, 0), Direction::Down),
(Position::new(1, 7), Direction::Up), (Position::new(1, 7), Direction::Up),
); );
b.set_blocked(4, 0, true);
b.set_blocked_range(0, 2, 5, 5, true); b.set_blocked_range(0, 2, 5, 5, true);
b.build() b.build()
@ -717,29 +742,31 @@ pub mod problems {
(Position::new(0, 0), Direction::Down), (Position::new(0, 0), Direction::Down),
(Position::new(5, 5), Direction::Down), (Position::new(5, 5), Direction::Down),
); );
b.set_blocked(0, 0, true);
b.add_path( b.add_path(
(Position::new(0, 5), Direction::Up), (Position::new(0, 5), Direction::Up),
(Position::new(5, 0), Direction::Up), (Position::new(5, 0), Direction::Up),
); );
b.set_blocked(0, 0, true);
b.build() b.build()
} }
pub fn snake() -> Bruteforce { pub fn snake() -> Bruteforce {
let mut p = BruteforceBuilder::new(14, 3); let mut p = BruteforceBuilder::new(13, 3);
p.add_path( p.add_path(
(Position::new(0, 0), Direction::Right), (Position::new(0, 0), Direction::Right),
(Position::new(13, 0), Direction::Right), (Position::new(12, 0), Direction::Right),
); );
p.add_path( p.add_path(
(Position::new(0, 1), Direction::Right), (Position::new(0, 1), Direction::Right),
(Position::new(13, 1), Direction::Right), (Position::new(12, 1), Direction::Right),
); );
p.add_path( p.add_path(
(Position::new(0, 2), Direction::Right), (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); p.set_blocked_range(3, 2, 10, 2, true);

View file

@ -225,11 +225,7 @@ impl ConflictAvoidance {
// edge cases with underground into and end // edge cases with underground into and end
if let Some(PathField::Underground { if let Some(PathField::Underground { pos, dir, len }) = path.get(end_index + 1)
pos,
dir: _,
len: _,
}) = path.get(end_index + 1)
{ {
if xrange.contains(&(pos.x as usize)) && yrange.contains(&(pos.y as usize)) if xrange.contains(&(pos.x as usize)) && yrange.contains(&(pos.y as usize))
{ {
@ -237,9 +233,38 @@ impl ConflictAvoidance {
println!("Blocked {:?}", p); println!("Blocked {:?}", p);
b.set_blocked(p.x as usize, p.y as usize, true); b.set_blocked(p.x as usize, p.y as usize, true);
} }
for l in 1..*len {
let p = pos.in_direction(dir, l as i32);
if xrange.contains(&(p.x as usize)) && yrange.contains(&(p.y as usize))
{
let p = p - offset;
b.set_underground(p.x as usize, p.y as usize, Some(*dir));
}
}
} }
b.add_path((start_pos - offset, start_dir), (end_pos - offset, end_dir)); let start_pos_offset = start_pos - offset;
b.set_blocked(
start_pos_offset.x as usize,
start_pos_offset.y as usize,
true,
);
if let PathField::Underground { pos, dir, len } = path[start_index] {
for l in 1..len {
let p = pos.in_direction(&dir, l as i32);
if xrange.contains(&(p.x as usize)) && yrange.contains(&(p.y as usize))
{
let p = p - offset;
b.set_underground(p.x as usize, p.y as usize, Some(dir));
}
}
}
b.add_path((start_pos_offset, start_dir), (end_pos - offset, end_dir));
mapping.push((i, start_index, end_index)); mapping.push((i, start_index, end_index));
} }
@ -249,6 +274,7 @@ impl ConflictAvoidance {
let mut b = b.build(); let mut b = b.build();
println!("{}", b);
println!("{}", self); println!("{}", self);
let mut min_cost = f64::INFINITY; let mut min_cost = f64::INFINITY;
@ -274,14 +300,7 @@ impl ConflictAvoidance {
t.extend_from_slice(&self.belts[index][0..=start]); t.extend_from_slice(&self.belts[index][0..=start]);
t.extend(p[1..].iter().map(|p| p.offset(&offset))); t.extend(p[1..].iter().map(|p| p.offset(&offset)));
let (end_pos, end_dir) = self.belts[index][end].end_pos(); t.extend_from_slice(&self.belts[index][end + 1..]);
t.push(PathField::Belt {
pos: end_pos,
dir: end_dir,
});
if end + 1 < self.belts[index].len() {
t.extend_from_slice(&self.belts[index][end + 1..]);
}
// println!("{:?}", &t); // println!("{:?}", &t);
// println!(); // println!();
// println!("{:?}", &self.belts[index]); // println!("{:?}", &self.belts[index]);