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

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.

2

u/FromBeyond Dec 16 '16

You can make it much faster by just working with bool[] or List<bool>. Basically what I did to keep mine from taking until the heat death of the universe for part 2.

var input = "11101000110010100".Select(x => x == '1').ToList();
  
var requiredLength = 35651584;

while(input.Count() < requiredLength)
{
  var a = input;

  var bBuilder = a.Select(x => !x).Reverse();
  
  input = new List<bool>(a);
  input.Add(false);
  input.AddRange(bBuilder);
}

input = input.Take(requiredLength).ToList();

var checkSum = new List<bool>();

while(checkSum.Count() % 2 == 0)
{
  var tempCheckSum = new List<bool>();

  for(var i = 0; i < input.Count(); i += 2)
  {
    if(i + 1 >= input.Count())
    {
      break;
    }
    
    tempCheckSum.Add((input[i] == input[i + 1]));
  }

  input = tempCheckSum;
  checkSum = tempCheckSum;
}

System.Console.WriteLine(string.Join("", checkSum.Select(x => x ? '1' : '0')));     

2

u/scottishrob13 Dec 17 '16

That was a good idea. I was down to ~1.5 seconds and cut that into ~.5. I could probably go even lower if I hard-coded my input and flipped input as lists instead of converting them from strings every time.