aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-03 18:05:47 +1000
committerGravatar alecdwm 2019-12-03 18:05:47 +1000
commit0c4726f6f13c2457bb81469466360bcaf7f1fbe1 (patch)
treecb978cee8dfbb46d373b748e6e67e537e27afc83
parent143b6fdef5d334b273d7106dc068d2ed4819ae05 (diff)
fixed 2019 day3 part1 solution wires missing first line segment
-rw-r--r--src/year_2019/day3.rs24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/year_2019/day3.rs b/src/year_2019/day3.rs
index 5fa4d6c..676b770 100644
--- a/src/year_2019/day3.rs
+++ b/src/year_2019/day3.rs
@@ -63,11 +63,19 @@ pub fn part1() {
);
}
-#[derive(Debug, Default)]
+#[derive(Debug)]
struct Wire {
points: Vec<Point>,
}
+impl Default for Wire {
+ fn default() -> Self {
+ Self {
+ points: vec![Point::zero()],
+ }
+ }
+}
+
impl Wire {
fn parse_wires(serialized: &str) -> Vec<Self> {
serialized.trim().split("\n").map(Wire::from).collect()
@@ -110,7 +118,11 @@ impl Wire {
&& min_y <= target_y
&& target_y <= max_y
{
- intersections.push(Point::new(x, target_y));
+ let intersection = Point::new(x, target_y);
+ if intersection == Point::zero() {
+ continue;
+ }
+ intersections.push(intersection);
}
} else {
assert_eq!(min_y, max_y);
@@ -123,7 +135,11 @@ impl Wire {
&& min_x <= target_x
&& target_x <= max_x
{
- intersections.push(Point::new(target_x, y));
+ let intersection = Point::new(target_x, y);
+ if intersection == Point::zero() {
+ continue;
+ }
+ intersections.push(intersection);
}
}
}
@@ -197,7 +213,7 @@ impl From<char> for WireSegmentDirection {
}
}
-#[derive(Debug, Default, Copy, Clone)]
+#[derive(Debug, Default, PartialEq, Copy, Clone)]
struct Point {
x: i64,
y: i64,