Skip to content

Commit

Permalink
2019: Implement AddAssign trait for Point
Browse files Browse the repository at this point in the history
Allow for the `+=` operator to be used to self-modify the Point struct.
  • Loading branch information
ericvw committed Apr 25, 2024
1 parent 3edce64 commit 2c55891
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 2019/src/bin/puzzle_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn trace_path(path: &Vec<Vector>) -> HashMap<Point, u32> {
};

for _ in 0..magnitude {
c = c + step;
c += step;
len += 1;
trace.entry(c).or_insert(len);
}
Expand Down
10 changes: 10 additions & 0 deletions 2019/src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Sub;

#[derive(Debug, Default, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
Expand All @@ -24,6 +25,15 @@ impl Add for Point {
}
}

impl AddAssign for Point {
fn add_assign(&mut self, other: Self) {
*self = Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}

impl Sub for Point {
type Output = Self;

Expand Down

0 comments on commit 2c55891

Please sign in to comment.