r/adventofcode • u/daggerdragon • Dec 16 '16
SOLUTION MEGATHREAD --- 2016 Day 16 Solutions ---
--- Day 16: Dragon Checksum ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
DRINKING YOUR OVALTINE IS MANDATORY [?]
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!
5
Upvotes
View all comments
Show parent comment
2
u/Smylers Dec 16 '16
Good idea. (Thanks.)
For reducing each chunk, you don't need to process it pairwise: simply count the number of 1s in the entire chunk and see if it's odd or even: if there's an odd number of 1s then somewhere there's a 1 which didn't pair with another 1, so the chunk as a whole ends up as 0.
Perl implementation:
&= ~1forces the fill length to be even (since any trailing odd character plays no part in the checksum).The length of each chunk needs to be the biggest power of 2 that's a factor of the length of the data. I'm not clever enough to know how to calculate that directly, but powers of 2 always end in 0s in binary, so I convert the length to a string of its binary representation, grab the right-most 1 followed by however many 0s, and convert that back to a number.
Then for each chunk, count the 1s, reduce to a single bit, and flip it.