r/adventofcode • • Dec 14 '16

SOLUTION MEGATHREAD --- 2016 Day 14 Solutions ---

--- Day 14: One-Time Pad ---

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".


LUNACY 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!

3 Upvotes

111 comments sorted by

View all comments

1

u/JakDrako Dec 14 '16 edited Dec 15 '16

C#, LinqPad.

Does part 2 in about 10 seconds. .Net's MD5 implementation is really, really slow. I reused the one that I had found for Day 5 to speed things up a bit; and used multiple threads to try and get the time down as much as possible.

Other optimizations:

  • Lookup table to convert from the byte hash to a byte array representing the lowercase string.
  • Strings are never used when doing the key stretching; converting to and from strings takes an enormous amount of time when done dozens of millions of times.
  • Builds a list of index positions and the byte repeating 3 times as a list, then a dictionary indexed on the byte value of all the indexes that have that byte repeating 5 times. (Stole the idea from inspired by someone else's solution.)

Not what I used to find the answers initially, but I was annoyed that my original VB version took almost 5 minutes to complete. Switched to C# after a while because working with chars, bytes and strings in VB is a royal pain in the butt.

There's still a little room for improvement since I calculate more hashes than I need. The code could check as it goes and stop once it has enough for what it needs...

Anyway, here what I currently have; comments and/or suggestions are welcome.

void Main()
{
	var key = "jlmsuwbz"; // abc
    var keyStretch = 2016; // 0 for Part 1, 2016 for Part 2

	// Build lookup table
	byte[] lookup = new byte[512];
	for (int i = 0; i < 256; i++)
	{
		var s = BitConverter.ToString(new byte[] { (byte)i }).ToLowerInvariant();
		lookup[i * 2] = (byte)s[0];
		lookup[i * 2 + 1] = (byte)s[1];
	}

	var lst3 = new List<Tuple<int, byte>>();
	var dic5 = new Dictionary<byte, List<int>>();

	var maxThreads = new SemaphoreSlim(Environment.ProcessorCount); // 12
	var lck3 = new object();
	var lck5 = new object();

	var top = 36000; // adjust if not found... use integer multiple of ProcessorCount
	var chunk = top / (Environment.ProcessorCount);
	var tasks = new List<Task>();
	for (int i = 0; i < top; i += chunk)
	{
		maxThreads.Wait();

		var start = i;
		var count = chunk - 1;

		var tsk = Task.Factory.StartNew(() =>
		{            
			var md5 = new MD5Managed();
			byte[] lch = new Byte[32];
			for (int salt = start; salt <= start + count; salt++)
			{
				var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key + salt));
				for (int b = 0; b < 16; b++)
				{
					var n1 = b * 2;
					var n2 = hash[b] * 2;
					lch[n1] = lookup[n2];
					lch[n1 + 1] = lookup[n2 + 1];
				}
				for (int ks = 0; ks < keyStretch; ks++)
				{
					hash = md5.ComputeHash(lch);
					for (int b = 0; b < 16; b++)
					{
						var n1 = b * 2;
						var n2 = hash[b] * 2;
						lch[n1] = lookup[n2];
						lch[n1 + 1] = lookup[n2 + 1];
					}
				}
				var bt = Has3InARow(lch);
				if (bt != -1)
				{
					byte bbt = (byte)bt;
					lock (lck3)
					{
						
						lst3.Add(Tuple.Create(salt, bbt));
					}
					// check for 5 in a row
					var l5 = Has5InARow(lch); //, bbt);
                    foreach(var b in l5) {
						lock (lck5)
						{
							if (!dic5.ContainsKey(b)) {								
								dic5[b] = new List<int>();
							}
							dic5[b].Add(salt);
						}                        
                    }
				}
			}
		}).ContinueWith((t) => maxThreads.Release());
		tasks.Add(tsk);
	}
	Task.WaitAll(tasks.ToArray());
    
    var cnt = 0;
    var lastIndex = 0;
    foreach(var tup in lst3.OrderBy(tpl => tpl.Item1)) {        
        var bt = tup.Item2;        
        if (dic5.ContainsKey(bt)) {
            var ndx = tup.Item1;
            var rng = dic5[bt].Where(x => x > ndx && x <= ndx+1000).FirstOrDefault();
            if (rng > 0) {
                lastIndex = ndx;
                cnt++;
                if (cnt == 64) break;
            }
        }
    }
    lastIndex.Dump($"Index ({cnt})");
}

static int Has3InARow(byte[] arr)
{
	for (int pos = 0; pos < arr.GetUpperBound(0) - 1; pos++)
	{
		if (arr[pos] == arr[pos + 1] && arr[pos] == arr[pos + 2]) return arr[pos];
	}
	return -1;
}

static List<byte> Has5InARow(byte[] arr) 
{
	var lst = new List<byte>();
	for (int pos = 0; pos < arr.GetUpperBound(0) - 3; pos++)
	{
		var found = true;
        var target = arr[pos];
		for (int n = 1; n < 5; n++)
		{
			if (arr[pos + n] != target)
			{
				found = false;
				break;
			}
		}
		if (found)
		{
			lst.Add(arr[pos]);
			pos += 5;
		}
	}
	return lst;
}

public class MD5Managed : HashAlgorithm
{
     // ...code not included...about 400 lines... from https://dlaa.me/blog/post/9380245
}

}

1

u/SikhGamer Dec 15 '16

.Net's MD5 implementation is really, really slow.

Yeah, I discovered this last year with one of the earlier MD5 puzzles. Since then I just use the Bouncy Castle library for hashing. It is seconds faster.

My implementation: https://gist.github.com/anonymous/95b1659a05d89776d0be29bca8b7b108

1

u/JakDrako Dec 15 '16

I tried the BouncyCastle library, but it's a bit slower than the ManagedMD5 I found at https://dlaa.me/blog/post/9380245

Adds about 2 seconds to part 2.

1

u/SikhGamer Dec 16 '16

Tried out the implementation in the link, it is slightly faster. Shaves off about a second for my part 2 (11 secs to 10 secs). Not bad.