use crate::util::parse::*;
type Input = (usize, usize);
pub fn parse(input: &str) -> Input {
parse_node(&mut input.iter_unsigned(), &mut Vec::new())
}
pub fn part1(input: &Input) -> usize {
input.0
}
pub fn part2(input: &Input) -> usize {
input.1
}
fn parse_node(iter: &mut impl Iterator<Item = usize>, stack: &mut Vec<usize>) -> (usize, usize) {
let child_count = iter.next().unwrap();
let metadata_count = iter.next().unwrap();
let mut metadata = 0;
let mut score = 0;
for _ in 0..child_count {
let (first, second) = parse_node(iter, stack);
metadata += first;
stack.push(second);
}
for _ in 0..metadata_count {
let n = iter.next().unwrap();
metadata += n;
if child_count == 0 {
score += n;
} else if n > 0 && n <= child_count {
score += stack[stack.len() - child_count + (n - 1)];
}
}
stack.truncate(stack.len() - child_count);
(metadata, score)
}