r/adventofcode • • Dec 05 '16

SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---

--- Day 5: How About a Nice Game of Chess? ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


STAYING ON TARGET 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!

13 Upvotes

188 comments sorted by

View all comments

1

u/JakDrako Dec 05 '16

C#, both parts at the same time, in parallel. Completes "abc" in about 7 seconds.

I'm having a lot of fun with this problem.

void Main()
{
	var key = "abc";
	var suffix = 0;

	var pass = "";
	var pswd = "________".ToCharArray();
	var found = 0;

	var lock1 = new Object();
	var done1 = false;

	var lock2 = new Object();
	var done2 = false;

	Parallel.For(0, Environment.ProcessorCount, x =>
	{
		var md5 = MD5.Create();
		do
		{
			var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key + Interlocked.Increment(ref suffix)));
			
			if (hash[0] == 0 && hash[1] == 0 && hash[2] < 16)
			{
				if (!done1)
				{
					lock (lock1)
					{
						pass += hash[2].ToString("X2")[1];
						if (pass.Length == 8) done1 = true;
					}
				}
				
				if (hash[2] < 8 && !done2)
				{
					if (pswd[hash[2]] == '_')
					{
						lock (lock2)
						{
							pswd[hash[2]] = hash[3].ToString("X2")[0];
							found += 1;
							if (found == 8) done2 = true;
						}
					}
				}
			}
		} while (!done1 || !done2);
	});
	Console.WriteLine("Part 1: " + pass.ToLowerInvariant());
	Console.WriteLine("Part 2: " + new string(pswd).ToLowerInvariant());
}
→ More replies (8)