summaryrefslogtreecommitdiff
path: root/2018/src/day8.rs
blob: 81f0d0b6a8432c0c65500aa65183697a4f2b28be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::io;
use std::io::BufRead;

pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> {
    #[derive(PartialEq, Eq)]
    struct Entry {
        nch: i32,
        nmeta: i32,
    }

    let mut stk = vec![Entry {nch: 1, nmeta: 0}];
    let mut total_meta = 0;
    let mut vals: Vec<Vec<i32>> = vec![vec![]];
    let mut current_val = 0;

    for num in reader.lines().next().unwrap().unwrap().split(' ').map(|s| s.parse::<i32>().unwrap()) {
        // Yay non-lexical lifetimes!
        let l = stk.last_mut().unwrap();
        if l.nmeta == -1 {
            // println!("read nmeta {}", num);
            l.nmeta = num;
            continue;
        }

        if l.nmeta == 0 && l.nch == 0 {
            loop {
                // println!("pop");
                stk.pop();

                // println!("  node value {}", current_val);
                vals.pop();
                vals.last_mut().unwrap().push(current_val);
                current_val = 0;

                let l = stk.last().unwrap();
                if l.nmeta != 0 || l.nch != 0 { break; }
            }
        }

        let l = stk.last_mut().unwrap();
        if l.nch == 0 {
            // println!("+meta {}", num);
            total_meta += num;

            if vals.last().unwrap().len() == 0 {
                // println!("  add value");
                current_val += num;
            } else if 1 <= num && num <= vals.last().unwrap().len() as i32 {
                // println!("  add child value {}", vals.last().unwrap()[num as usize - 1]);
                current_val += vals.last().unwrap()[num as usize - 1];
            }

            l.nmeta -= 1;
        } else {
            // println!("read nch {}", num);
            l.nch -= 1;
            stk.push(Entry {nch: num, nmeta: -1});
            vals.push(vec![]);
        }
    }

    let part1 = total_meta.to_string();
    let part2 = current_val.to_string();

    Ok((part1, part2))
}