mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Extract out function to build orbit tree
This commit is contained in:
parent
93d609a25e
commit
69c072e211
1 changed files with 9 additions and 4 deletions
|
@ -29,6 +29,14 @@ fn parse_input(input: &str) -> Vec<Orbit<'_>> {
|
|||
}
|
||||
|
||||
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<Node<'a>> {
|
||||
// Build the tree from the input
|
||||
let mut nodes: Vec<Node<'_>> = 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 {
|
||||
|
|
Loading…
Reference in a new issue