added 08-18
This commit is contained in:
parent
f616a8f164
commit
e7c5cff316
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
target
|
||||
target
|
||||
debug.log
|
||||
1
ressources/2018/day8
Normal file
1
ressources/2018/day8
Normal file
File diff suppressed because one or more lines are too long
91
src/advent2018/day8.rs
Normal file
91
src/advent2018/day8.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||
|
||||
use crate::helper::{
|
||||
debug::{truncate_file, write_to_file},
|
||||
file::read_file,
|
||||
};
|
||||
|
||||
fn get_input_data() -> Vec<u32> {
|
||||
let input = PathBuf::from("ressources/2018/day8");
|
||||
let input = read_file(input).unwrap();
|
||||
input_vec(input.as_str())
|
||||
}
|
||||
#[test]
|
||||
fn testdata_pt1() {
|
||||
let testdata = input_vec("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2");
|
||||
let nodes = parse_node(&mut testdata.into_iter());
|
||||
let sum = sum_metadata(&nodes);
|
||||
assert_eq!(sum, 138);
|
||||
}
|
||||
#[test]
|
||||
fn testdata_pt2() {
|
||||
let testdata = input_vec("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2");
|
||||
let nodes = parse_node(&mut testdata.into_iter());
|
||||
assert_eq!(nodes.borrow().value, 66);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_pt1() {
|
||||
truncate_file();
|
||||
let input = get_input_data();
|
||||
let nodes = parse_node(&mut input.into_iter());
|
||||
let sum = sum_metadata(&nodes);
|
||||
assert_eq!(sum, 44893);
|
||||
}
|
||||
#[test]
|
||||
fn run_pt2() {
|
||||
truncate_file();
|
||||
let input = get_input_data();
|
||||
let nodes = parse_node(&mut input.into_iter());
|
||||
assert_eq!(nodes.borrow().value, 27433);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node {
|
||||
children: Vec<Rc<RefCell<Node>>>,
|
||||
metadata: Vec<u32>,
|
||||
value: u32,
|
||||
}
|
||||
|
||||
fn input_vec(input: &str) -> Vec<u32> {
|
||||
input
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse::<u32>().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_node(mut iter: &mut impl Iterator<Item = u32>) -> Rc<RefCell<Node>> {
|
||||
let (children_count, metadata_count) = (iter.next().unwrap(), iter.next().unwrap());
|
||||
let (mut children, mut metadata) = (Vec::new(), Vec::new());
|
||||
|
||||
for i in 0..children_count {
|
||||
let child = parse_node(iter);
|
||||
children.push(child);
|
||||
}
|
||||
|
||||
let mut value = 0;
|
||||
for j in 0..metadata_count {
|
||||
let meta_value = iter.next().unwrap();
|
||||
if meta_value > 0 && meta_value as usize <= children.len() {
|
||||
value += children[meta_value as usize - 1].borrow().value;
|
||||
}
|
||||
metadata.push(meta_value);
|
||||
}
|
||||
|
||||
if children_count == 0 {
|
||||
value = metadata.iter().sum();
|
||||
}
|
||||
Rc::new(RefCell::new(Node {
|
||||
children,
|
||||
metadata,
|
||||
value,
|
||||
}))
|
||||
}
|
||||
|
||||
fn sum_metadata(node: &Rc<RefCell<Node>>) -> u32 {
|
||||
let mut sum = node.borrow().metadata.iter().sum::<u32>();
|
||||
for child in node.borrow().children.iter() {
|
||||
sum += sum_metadata(child);
|
||||
}
|
||||
sum
|
||||
}
|
||||
@ -5,3 +5,4 @@ pub mod day4;
|
||||
pub mod day5;
|
||||
pub mod day6;
|
||||
pub mod day7;
|
||||
pub mod day8;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
const FILE: &str = "./test_out";
|
||||
const FILE: &str = "./debug.log";
|
||||
|
||||
pub fn write_to_file(content: &str) {
|
||||
pub fn write_to_file<T: AsRef<str>>(content: T) {
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
@ -8,7 +8,7 @@ pub fn write_to_file(content: &str) {
|
||||
.append(true)
|
||||
.open(FILE)
|
||||
.unwrap();
|
||||
writeln!(file, "{}", content);
|
||||
writeln!(file, "{}", content.as_ref());
|
||||
}
|
||||
|
||||
pub fn write_to_file_noln(content: &str) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user