Day 3 part 2

This commit is contained in:
Wesley Moore 2019-12-03 21:13:31 +11:00
parent bdf11def27
commit 7d282f886f
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE

View file

@ -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(())
}