Commit b3de136c authored by Jonathan Klimt's avatar Jonathan Klimt

Part 1 of day 8

parent 6aef8501
[[package]]
name = "day8"
version = "0.1.0"
[package]
name = "day8"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
edition = "2018"
[dependencies]
This diff is collapsed.
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
input 2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
index 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
A--------|--|--|----------|-|-|-|-|
B----------- C-----------
D-----
use std::io::Error;
use std::fs;
type Node = Vec<usize>;
fn add_node(n: & mut Vec<Node>, inp: &Vec<usize>) -> usize {
// println!("Input: {:?}", inp);
let mut new_node : Node = Node::new();
let nr_children = inp[0];
let nr_metadata = inp[1];
if nr_children == 0 {
for i in 2..2+nr_metadata{
new_node.push(inp[i]);
}
n.push(new_node);
return nr_metadata + 2;
}
let mut payload_index = 2;
for _ in 0..nr_children {
let smaller_input : Vec<usize> = inp[payload_index..].to_vec();
payload_index += add_node(n, &smaller_input);
}
for i in payload_index..payload_index + nr_metadata {
new_node.push(inp[i]);
payload_index += 1;
}
n.push(new_node);
payload_index
}
fn main() -> Result<(), Error> {
let f = fs::read_to_string("input.txt")?;
let mut inp_vec : Vec<usize> = Vec::new();
let mut node_vec : Vec<Node> = Vec::new();
for line in f.lines() {
let mut tmp_inp_vec : Vec<usize> = line.split(' ').map(|s| s.parse::<usize>().unwrap()).collect();
inp_vec.append(&mut tmp_inp_vec);
}
add_node(&mut node_vec, &inp_vec);
let sum = node_vec.into_iter().flatten().sum::<usize>();
println!("Sum: {:?}", sum);
Ok(())
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment