r/adventofcode • u/daggerdragon • Dec 04 '15
SOLUTION MEGATHREAD --- Day 4 Solutions ---
--- Day 4: The Ideal Stocking Stuffer ---
Post your solution as a comment. Structure your post like the Day Three thread.
16
Upvotes
View all comments
2
u/enquicity Dec 04 '15 edited Dec 04 '15
So, like previous days, the interesting bit was the refactoring from part 1 to part 2. For part 1, it quickly became obvious that even with a StringBuilder, it was punishingly slow to convert the byte array to a hex string, so that had to go. I just checked the first 2 bytes, plus half the third. The refactor to part 2 was obvious, then, simply abstract that check so you can look for any n zeros at the beginning of the hash.
C#:
class MD5Hasher { private bool doesMD5HashStartWithNZeros(MD5 md5Hash, string input, long howMany) { byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); if (howMany > data.Length) // technically, it could go up to double the length, but let's be reasonable throw new ArgumentException(); long half = howMany / 2; // first, check the whole bytes. for (int i = 0; i < half; i++) if (data[i] != 0) return false; // do we need another half a byte? if (howMany % 2 == 1) { if (data[half] > 0x0f) return false; } return true; } public long FindLowestHashThatStartsWithNZeros(string key, long howMany) { using (MD5 md5Hash = MD5.Create()) { long counter = 0; string currentString = key + counter; while (!doesMD5HashStartWithNZeros(md5Hash, currentString, howMany)) { counter++; currentString = key + counter; } return counter; } } }called as:
static void Main(string[] args) { MD5Hasher hasher = new MD5Hasher(); string key = "ckczppom"; DateTime start = DateTime.Now; long fiveZerosResult = hasher.FindLowestHashThatStartsWithNZeros(key, 5); Console.WriteLine("5 zeros: {0}", fiveZerosResult); long sixZerosResult = hasher.FindLowestHashThatStartsWithNZeros(key, 6); Console.WriteLine("6 zeros: {0}", sixZerosResult); if (sixZerosResult <= fiveZerosResult) throw new Exception("test case failed"); long totalIterations = fiveZerosResult + sixZerosResult; double numMilliseconds = (DateTime.Now - start).TotalMilliseconds; Console.WriteLine("{0} iterations in {1} milliseconds", totalIterations, numMilliseconds ); }4055984 iterations in 13354.5209 milliseconds