day23 part 2 correct intersection test (speed up by factor 2, yay!)
This commit is contained in:
@@ -138,41 +138,6 @@ impl Bot {
|
|||||||
fn dist(&self, p: &Point) -> isize {
|
fn dist(&self, p: &Point) -> isize {
|
||||||
self.center.distance(&p)
|
self.center.distance(&p)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn corners(&self) -> Vec<Point> {
|
|
||||||
vec![
|
|
||||||
Point {
|
|
||||||
x: self.center.x + self.range as isize,
|
|
||||||
y: self.center.y,
|
|
||||||
z: self.center.z,
|
|
||||||
},
|
|
||||||
Point {
|
|
||||||
x: self.center.x - self.range as isize,
|
|
||||||
y: self.center.y,
|
|
||||||
z: self.center.z,
|
|
||||||
},
|
|
||||||
Point {
|
|
||||||
x: self.center.x,
|
|
||||||
y: self.center.y + self.range as isize,
|
|
||||||
z: self.center.z,
|
|
||||||
},
|
|
||||||
Point {
|
|
||||||
x: self.center.x,
|
|
||||||
y: self.center.y - self.range as isize,
|
|
||||||
z: self.center.z,
|
|
||||||
},
|
|
||||||
Point {
|
|
||||||
x: self.center.x,
|
|
||||||
y: self.center.y,
|
|
||||||
z: self.center.z + self.range as isize,
|
|
||||||
},
|
|
||||||
Point {
|
|
||||||
x: self.center.x,
|
|
||||||
y: self.center.y,
|
|
||||||
z: self.center.z - self.range as isize,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
@@ -229,69 +194,16 @@ impl Cube {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn corners(&self) -> Vec<Point> {
|
|
||||||
vec![
|
|
||||||
Point::new(
|
|
||||||
self.base.x + self.len,
|
|
||||||
self.base.y + self.len,
|
|
||||||
self.base.z + self.len,
|
|
||||||
),
|
|
||||||
Point::new(
|
|
||||||
self.base.x + self.len - 1,
|
|
||||||
self.base.y + self.len - 1,
|
|
||||||
self.base.z,
|
|
||||||
),
|
|
||||||
Point::new(
|
|
||||||
self.base.x + self.len - 1,
|
|
||||||
self.base.y,
|
|
||||||
self.base.z + self.len - 1,
|
|
||||||
),
|
|
||||||
Point::new(self.base.x + self.len - 1, self.base.y, self.base.z),
|
|
||||||
Point::new(
|
|
||||||
self.base.x,
|
|
||||||
self.base.y + self.len - 1,
|
|
||||||
self.base.z + self.len - 1,
|
|
||||||
),
|
|
||||||
Point::new(self.base.x, self.base.y + self.len - 1, self.base.z),
|
|
||||||
Point::new(self.base.x, self.base.y, self.base.z + self.len - 1),
|
|
||||||
Point::new(self.base.x, self.base.y, self.base.z),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn intersects(&self, bot: &Bot) -> bool {
|
fn intersects(&self, bot: &Bot) -> bool {
|
||||||
if self
|
((self.base.x - bot.center.x).abs()
|
||||||
.corners()
|
+ (self.base.x + self.len - bot.center.x).abs()
|
||||||
.iter()
|
+ (self.base.y - bot.center.y).abs()
|
||||||
.any(|corner| corner.distance(&bot.center) <= bot.range)
|
+ (self.base.y + self.len - bot.center.y).abs()
|
||||||
{
|
+ (self.base.z - bot.center.z).abs()
|
||||||
return true;
|
+ (self.base.z + self.len - bot.center.z).abs()
|
||||||
}
|
- 3 * self.len)
|
||||||
if bot
|
/ 2
|
||||||
.corners()
|
<= bot.range
|
||||||
.iter()
|
|
||||||
.any(|corner| self.contains_point(&corner))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// WARNING this is a conservative check! it omits the case where no
|
|
||||||
// corners of the octahedron/cube are within each other:
|
|
||||||
// +-----+
|
|
||||||
// | |/\
|
|
||||||
// | /| \
|
|
||||||
// +---/-+ \
|
|
||||||
// although this is just a wrong 2D example, in 3D that could happen.
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn contains_point(&self, p: &Point) -> bool {
|
|
||||||
self.base.x <= p.x
|
|
||||||
&& self.base.x + self.len > p.x
|
|
||||||
&& self.base.y <= p.y
|
|
||||||
&& self.base.y + self.len > p.y
|
|
||||||
&& self.base.z <= p.z
|
|
||||||
&& self.base.z + self.len > p.z
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user