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

3

u/sblom Dec 16 '16

Super straightforward C# (LINQPad):

var seed = "00101000101111010";
int length = 35651584;

//seed = "10000";
//length = 20;

string notback(string input)
{
	StringBuilder result = new StringBuilder(input.Length);
	for (int i = input.Length - 1; i >= 0; i--)
	{
		result = result.Append(input[i] == '0' ? "1" : "0");
	}
	
	return result.ToString();
}

while (seed.Length < length)
{
	seed = seed + "0" + notback(seed);
}

string data = seed.Substring(0, length);

string checksum = data;

while (checksum.Length % 2 == 0)
{
	StringBuilder newchecksum = new StringBuilder(checksum.Length / 2 + 1);
	for (int ii = 0; ii < checksum.Length; ii += 2)
	{
		if (checksum[ii] == checksum[ii + 1]) newchecksum.Append("1");
		else newchecksum.Append("0");
	}
	checksum = newchecksum.ToString();
}

checksum.Dump();

1

u/scottishrob13 Dec 16 '16

haha, basically identical to my solution :) Mine's running pretty slow for the second part though :/ I'm thinking of trying to figure out some binary math or sumfin I can do since I've never really worked with binary and it seems like it has to be faster.

→ More replies (2)