r/adventofcode • • 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

116 comments sorted by

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:

use v5.14;
use warnings;

my $fill_length = shift // 20;
my $data = shift // '10000';

$fill_length &= ~1;
$data .= '0' . reverse $data =~ tr/01/10/r while length $data < $fill_length;
(substr $data, $fill_length) = '';

(sprintf '%b', $fill_length) =~ /(10+)$/;
my $chunk_size = oct "0b$1";
my $checksum;
$checksum .= tr/1// & 1 ^ 1 while $_ = substr $data, 0, $chunk_size, '';
say $checksum;

&= ~1 forces 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.

2

u/bpeel Dec 16 '16

Ah, good trick about counting the bits!

For the biggest power of two you can do the voodoo n&(n-1). I used to know that but I forgot about it! p_tseng’s answer uses this as well as the bit counting trick and also has a nice explanation. I hand the AoC crown to him/her. :)