From 6f06d5d7ca1945658dc8196b04a6aeb2efba4910 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Fri, 6 Dec 2019 12:00:37 +0100 Subject: [PATCH] 2019: day06: use a HashSet instead --- aoc2019/src/day06.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/aoc2019/src/day06.rs b/aoc2019/src/day06.rs index d42266e..0eaeff5 100644 --- a/aoc2019/src/day06.rs +++ b/aoc2019/src/day06.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::collections::HashSet; use std::iter; use aoc::err; @@ -64,17 +65,10 @@ fn part2(input: &str) -> Result { let succ = |key: &String| orbits.get(key).map(|val| val.clone()); - let you_path = iter::successors(Some("YOU".to_string()), succ).collect::>(); - let santa_path = iter::successors(Some("SAN".to_string()), succ).collect::>(); + let you_path = iter::successors(Some("YOU".to_string()), succ).collect::>(); + let santa_path = iter::successors(Some("SAN".to_string()), succ).collect::>(); - let common = you_path - .iter() - .rev() - .zip(santa_path.iter().rev()) - .filter(|(x, y)| x == y) - .count(); - - Ok(you_path.len() - common + santa_path.len() - common - 2) + Ok(you_path.symmetric_difference(&santa_path).count() - 2) } #[cfg(test)]