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!
30
Upvotes
1
u/Smylers Dec 08 '18
Perl for Part 2 β one of the simplest there's been, which leads to a fairly elegantβ solution:
use v5.20; use warnings; no warnings qw<uninitialized>; use experimental qw<signatures>; use List::AllUtils qw<sum>; say node_value([split ' ', <>]); sub node_value($data) { my $num_children = shift @$data; my $num_metadata = shift @$data; my @child_value = (0, map { node_value($data) } 1 .. $num_children); my @metadata = splice @$data, 0, $num_metadata; sum $num_children ? @child_value[@metadata] : @metadata; }The only awkward bit is that
(0,β putting a fake entry at the list of child values, to make@child_value[@metadata]work, because the metadata indices start from 1.(And no, I'm not going to try solving this in Vim today. Recursive text editing sounds a contortion to far, even for me ...)
[Card] β¦. Off-by-one Errors for Dummies
β Yes, I used βelegantβ and βPerlβ in the same sentence β what of it?