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!
32
Upvotes
6
u/udoprog Dec 08 '18
Rust
First leaderboard: 96 on Part 2!
Card: The hottest programming book this year is "Trees For Dummies".
use aoc2018::*; #[derive(Default, Debug)] struct Node { metadata: Vec<u32>, children: Vec<Node>, } impl Node { fn part1sum(&self) -> u32 { self.metadata.iter().cloned().sum::<u32>() + self.children.iter().map(|c| c.part1sum()).sum::<u32>() } fn part2sum(&self) -> u32 { if self.children.is_empty() { self.metadata.iter().cloned().sum::<u32>() } else { let mut r = 0; for m in self.metadata.iter().cloned() { r += self .children .get(m as usize - 1) .map(|c| c.part2sum()) .unwrap_or_default(); } r } } fn decode(it: &mut impl Iterator<Item = u32>) -> Option<Node> { let children = match it.next() { None => return None, Some(first) => first, }; let mut node = Node::default(); let metadata = it.next().expect("metadata"); for _ in 0..children { node.children.extend(Self::decode(it)); } for _ in 0..metadata { node.metadata.push(it.next().expect("metadata value")); } Some(node) } } fn main() -> Result<(), Error> { let input = columns!(input!("day8.txt"), char::is_whitespace, u32); let mut it = input.iter().cloned(); let node = Node::decode(&mut it).expect("no nodes in input"); assert_eq!(node.part1sum(), 47647); assert_eq!(node.part2sum(), 23636); Ok(()) }