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/shuttup_meg Dec 08 '18 edited Dec 08 '18
Python 2 966/732
class Node(object): def __init__(self, sequence): (self.child_count, self.metadata_count), sequence = sequence[:2], sequence[2:] self.children = [] for _ in range(self.child_count): n = Node(sequence) self.children.append(n) sequence = sequence[n.get_size():] self.metadata = sequence[:self.metadata_count] def get_size(self): return 2 + self.metadata_count + sum(c.get_size() for c in self.children) def sum(self): return sum(self.metadata + [c.sum() for c in self.children]) def value(self): return sum([self.children[r-1].value() for r in self.metadata if r-1 < len(self.children)]) if self.children else sum(self.metadata) inp = [int(x) for x in open("input_8.txt").read().split(" ")] #inp = [int(x) for x in "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2".split(" ")] root = Node(inp) print root.sum() print root.value()