day08 part 2

This commit is contained in:
Johannes Schaefer
2018-12-09 21:34:43 +01:00
parent 51418b709e
commit d84e3bff27
2 changed files with 18 additions and 4 deletions

View File

@@ -1,14 +1,14 @@
use crate::utils;
pub fn task1() {
pub fn both() {
let input: Vec<usize> = utils::read_file("input/day08.txt")
.split(" ")
.map(|x| x.parse().unwrap())
.collect();
let (tree, rest) = Node::extract_from(&input);
let (tree, _) = Node::extract_from(&input);
println!("Metadata sum: {}", tree.metadata_sum());
println!("rest: {:?}", rest);
println!("Task2 sum: {}", tree.task2());
}
struct Node {
@@ -44,4 +44,18 @@ impl Node {
})
.sum::<usize>()
}
fn task2(&self) -> usize {
if self.children.len() > 0 {
self.metadata
.iter()
.map(|meta| match self.children.get(*meta - 1) {
Some(child) => child.task2(),
None => 0,
})
.sum()
} else {
self.metadata_sum()
}
}
}