Use PositionType for position

This commit is contained in:
hal8174 2024-01-20 14:55:29 +01:00
parent 2e9c699600
commit 207a0436d8
4 changed files with 283 additions and 194 deletions

View file

@ -1,11 +1,12 @@
use std::{fmt::Display, ops::RangeInclusive};
use clap::builder::PathBufValueParser;
use colored::Colorize;
use crate::{belt_finding::brute_force::BruteforceBuilder, misc::Map};
use super::{
common::{Dimension, Direction, PathField, Position},
common::{Dimension, Direction, PathField, Position, PositionType},
Problem, COLORS,
};
@ -90,20 +91,13 @@ impl ConflictAvoidance {
for path in &self.belts {
for p in path {
match p {
PathField::Belt { pos, dir: _ } => *conflicts.get_mut(pos.x, pos.y) += 1,
PathField::Belt { pos, dir: _ } => {
*conflicts.get_mut(pos.x as usize, pos.y as usize) += 1
}
PathField::Underground { pos, dir, len } => {
*conflicts.get_mut(pos.x, pos.y) += 1;
let end = pos
.in_direction(
dir,
*len as usize,
&Dimension {
width: self.map.width,
height: self.map.height,
},
)
.unwrap();
*conflicts.get_mut(end.x, end.y) += 1;
*conflicts.get_mut(pos.x as usize, pos.y as usize) += 1;
let end = pos.in_direction(dir, *len as PositionType);
*conflicts.get_mut(end.x as usize, end.y as usize) += 1;
}
}
}
@ -181,13 +175,17 @@ impl ConflictAvoidance {
let s = path
.iter()
.map(|p| p.pos())
.position(|p| xrange.contains(&p.x) && yrange.contains(&p.y))
.position(|p| {
xrange.contains(&(p.x as usize)) && yrange.contains(&(p.y as usize))
})
.map(|i| i.saturating_sub(1));
let e = path
.iter()
.map(|p| p.pos())
.rev()
.position(|p| xrange.contains(&p.x) && yrange.contains(&p.y))
.position(|p| {
xrange.contains(&(p.x as usize)) && yrange.contains(&(p.y as usize))
})
.map(|i| usize::min(path.len() - 1, path.len() - i));
if let Some((start, mut end)) = s.zip(e) {
@ -198,12 +196,7 @@ impl ConflictAvoidance {
mapping.push((i, start, end));
let (start_pos, start_dir) = path[start]
.end_pos(&Dimension {
width: self.map.width,
height: self.map.height,
})
.unwrap();
let (start_pos, start_dir) = path[start].end_pos();
let end_pos = path[end].pos();
let end_dir = path[end].dir();
@ -213,15 +206,15 @@ impl ConflictAvoidance {
b.add_path(
(
Position::new(
start_pos.x - (xrange.start() - 1),
start_pos.y - (yrange.start() - 1),
start_pos.x - (*xrange.start() as i32 - 1),
start_pos.y - (*yrange.start() as i32 - 1),
),
start_dir,
),
(
Position::new(
end_pos.x - (xrange.start() - 1),
end_pos.y - (yrange.start() - 1),
end_pos.x - (*xrange.start() as i32 - 1),
end_pos.y - (*yrange.start() as i32 - 1),
),
*end_dir,
),
@ -255,7 +248,10 @@ impl ConflictAvoidance {
t.extend_from_slice(&self.belts[index][0..=start]);
t.extend(p[1..].iter().map(|p| {
p.offset(((*xrange.start() as i32) - 1, (*yrange.start() as i32) - 1))
p.offset(&Position::new(
(*xrange.start() as i32) - 1,
(*yrange.start() as i32) - 1,
))
}));
t.extend_from_slice(&self.belts[index][end..]);
// println!("{:?}", &t);
@ -305,39 +301,44 @@ impl Display for ConflictAvoidance {
for p in problem {
match p {
PathField::Belt { pos, dir } => match dir {
Direction::Up => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Right => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Down => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Left => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Up => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Right => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Down => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Left => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
},
PathField::Underground { pos, dir, len } => {
match dir {
Direction::Up => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Right => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Down => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Left => m.set(pos.x, pos.y, Some((i, ""))),
Direction::Up => m.set(pos.x as usize, pos.y as usize, Some((i, ""))),
Direction::Right => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
Direction::Down => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
Direction::Left => {
m.set(pos.x as usize, pos.y as usize, Some((i, "")))
}
};
let end_pos = pos
.in_direction(
dir,
*len as usize,
&Dimension {
width: self.map.width,
height: self.map.height,
},
)
.unwrap();
let end_pos = pos.in_direction(dir, *len as PositionType);
match dir {
Direction::Up => m.set(end_pos.x, end_pos.y, Some((i, ""))),
Direction::Right => m.set(end_pos.x, end_pos.y, Some((i, ""))),
Direction::Down => m.set(end_pos.x, end_pos.y, Some((i, ""))),
Direction::Left => m.set(end_pos.x, end_pos.y, Some((i, ""))),
Direction::Up => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Right => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Down => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
Direction::Left => {
m.set(end_pos.x as usize, end_pos.y as usize, Some((i, "")))
}
};
}
}
}
let last_pos = problem.last().unwrap().pos();
m.set(last_pos.x, last_pos.y, Some((i, "T")));
m.set(last_pos.x as usize, last_pos.y as usize, Some((i, "T")));
}
// Print body