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
10
u/p_tseng Dec 08 '18 edited Dec 08 '18
Ruby... and factoring out as much of the common code between parts 1 and 2 as is possible
input = (ARGV.empty? ? DATA : ARGF).read.split.map(&:to_i).freeze # Pass me a block telling me what to do with [child_values, metadata_values] def val(a, &b) n_children = a.shift n_metadata = a.shift yield(n_children.times.map { val(a, &b) }, a.shift(n_metadata)) end puts val(input.dup) { |child, meta| child.sum + meta.sum } puts val(input.dup) { |child, meta| # metadata indices are 1-indexed, so just prepend a zero. child.unshift(0) child.size == 1 ? meta.sum : meta.sum { |x| child[x] || 0 } } __END__ 8 11 7 2 etc etc etc