From 7d282f886f710042d3874bc54cbc5a41b798df81 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Tue, 3 Dec 2019 21:13:31 +1100 Subject: [PATCH] Day 3 part 2 --- 2019/src/bin/day3.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/2019/src/bin/day3.rs b/2019/src/bin/day3.rs index 6119ce4..4093c09 100644 --- a/2019/src/bin/day3.rs +++ b/2019/src/bin/day3.rs @@ -34,6 +34,19 @@ fn main() -> io::Result<()> { .unwrap(); println!("Part 1: {}", result); + // Part 2, count steps (length of path) + // For each common point find it's position in each wire paths, that is the steps + let result = wire1 + .intersection(&wire2) + .map(|point| { + paths[0].iter().position(|other| point == other).unwrap() + + paths[1].iter().position(|other| other == point).unwrap() + + 2 // to account for 0 based index + }) + .min() + .unwrap(); + println!("Part 2: {}", result); + Ok(()) }