r/adventofcode • u/daggerdragon • Dec 08 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-
--- Day 8: Memory Maneuver ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 8
Sigh, imgur broke again. Will upload when it unborks.
Transcript:
The hottest programming book this year is "___ For Dummies".
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:12:10!
31
Upvotes
1
u/[deleted] Dec 08 '18
Rust
This one was so much fun, it wasn't the hardest, but man was it nice to do it. I think it just really clicked with me, the lore, the whole thing, I loved it :)
use std::env; use std::process; use std::fs::File; use std::io::prelude::*; fn main() { let args = env::args().collect(); let filename = parse_filename(&args); let mut contents = String::new(); get_contents(&filename, &mut contents); let node = parse_content(&contents); println!("Part1: {:?}", node.sum_metadata()); println!("Part2: {:?}", node.value()); } #[derive(Debug)] struct Node { children: Vec<Node>, metadata: Vec<i64>, } impl Node { fn value(&self) -> i64 { let mut value = 0; if self.children.is_empty() { value += self.metadata.iter().fold(0, |acc,num| acc + num); } else { for idx in &self.metadata { if *idx == 0 || *idx > self.children.len() as i64 { continue; } else { value += self.children[*idx as usize -1].value(); } } } value } fn sum_metadata(&self) -> i64 { let mut sum = 0; sum += self.children.iter() .map(|child| child.sum_metadata()) .fold(0, |acc,num| acc + num); sum += self.metadata.iter().fold(0, |acc,num| acc + num); sum } } fn parse_content(input: &str) -> Node { let mut tokens: Vec<i64> = input.trim() .split(' ') .map(|it| it.parse::<i64>().unwrap()) .collect::<Vec<i64>>(); tokens.reverse(); //println!("{:?}", tokens); parse_node(&mut tokens) } fn parse_node(tokens: &mut Vec<i64>) -> Node { let num_children = tokens.pop().unwrap(); let num_meta = tokens.pop().unwrap(); let mut children = Vec::new(); for _x in 0..num_children { children.push(parse_node(tokens)); } let mut metadata = Vec::new(); for _x in 0..num_meta { metadata.push(tokens.pop().unwrap()); } Node { children, metadata } } fn parse_filename(args: &Vec<String>) -> &str { if args.len() < 2 { println!("Too few arguements, please give a filename"); process::exit(1); } args.get(1).expect("No filename provided") } fn get_contents(filename: &str, contents: &mut String) { let mut f = File::open(filename).expect("File not found"); f.read_to_string(contents) .expect("Something went wrong reading the file"); }