Skip to content

Commit

Permalink
Add ground check
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Jan 20, 2024
1 parent a63d2ee commit 56e43fa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions day22/src/day22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ struct Vec3 {
z: i32,
}

impl Vec3 {
fn new(x: i32, y: i32, z: i32) -> Self {
Self { x, y, z }
}
}

impl Add<Vec3> for Vec3 {
type Output = Self;

Expand Down Expand Up @@ -83,8 +77,9 @@ struct Brick {
}

impl Brick {
fn fall(&self) {
todo!()
fn fall(&self) -> Self {
let delta = Vec3 { x: 0, y: 0, z: -1 };
Self { start: self.start + delta, end: self.end + delta }
}

fn x_range(&self) -> RangeInclusive<i32> { self.start.x..=self.end.x }
Expand All @@ -99,6 +94,11 @@ impl Brick {

fn z_aligned(&self) -> bool { self.start.x == self.end.x && self.start.y == self.end.y }

fn on_ground(&self) -> bool {
assert!(self.start.z >= 0 && self.end.z >= 0);
self.start.z == 1 || self.end.z == 1
}

fn collides_with(&self, rhs: &Self) -> bool {
(self.x_aligned() && rhs.x_aligned() && intersects(self.x_range(), rhs.x_range()))
|| (self.y_aligned() && rhs.y_aligned() && intersects(self.y_range(), rhs.y_range()))
Expand All @@ -125,7 +125,7 @@ impl Board {
self.bricks
.iter()
.filter(|b| b.id != brick.id)
.any(|b| b.collides_with(&brick.value))
.any(|b| b.on_ground() || b.collides_with(&brick.value))
}

fn step(&mut self) {
Expand Down

0 comments on commit 56e43fa

Please sign in to comment.