From 69c072e211bdee9b002ace071c49c10f0c3c3f03 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sat, 7 Dec 2019 16:16:00 +1100 Subject: [PATCH] Extract out function to build orbit tree --- 2019/src/bin/day6.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/2019/src/bin/day6.rs b/2019/src/bin/day6.rs index b1622b2..1e50d25 100644 --- a/2019/src/bin/day6.rs +++ b/2019/src/bin/day6.rs @@ -29,6 +29,14 @@ fn parse_input(input: &str) -> Vec> { } fn number_of_orbits(input: &[Orbit<'_>]) -> usize { + let nodes = build_tree(input); + // Now traverse the node and sum the orbits of each one + (0..nodes.len()) + .map(|index| count_orbits(&nodes, index, 0)) + .sum() +} + +fn build_tree<'a>(input: &'a [Orbit<'a>]) -> Vec> { // Build the tree from the input let mut nodes: Vec> = Vec::with_capacity(input.len()); @@ -65,10 +73,7 @@ fn number_of_orbits(input: &[Orbit<'_>]) -> usize { }); } - // Now traverse the node and sum the orbits of each one - (0..nodes.len()) - .map(|index| count_orbits(&nodes, index, 0)) - .sum() + nodes } fn count_orbits(nodes: &[Node], node_index: usize, count: usize) -> usize {