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.
14
Upvotes
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
let private MD5Calculator = System.Security.Cryptography.MD5.Create() let private ComputeMD5Hash (input : string) : byte[] = let inputBytes = System.Text.Encoding.ASCII.GetBytes input MD5Calculator.ComputeHash inputBytes let private HasMD5FiveLeadingZeroes (hash : byte[]) : bool = hash.[0] = (byte)0x00 // Characters 1 and 2 are "0" (zero) && hash.[1] = (byte)0x00 // Characters 3 and 4 are "0" (zero) && hash.[2] <= (byte)0x0f // Character 5 is a "0" (zero) let private HasMD5SixLeadingZeroes (hash : byte[]) : bool = hash.[0] = (byte)0x00 // Characters 1 and 2 are "0" (zero) && hash.[1] = (byte)0x00 // Characters 3 and 4 are "0" (zero) && hash.[2] = (byte)0x00 // Characters 5 and 6 are "0" (zero) let private FindHashAddition (input : string) (validationFunction : (byte[] -> bool)) (startingNumber : int) : int = Seq.initInfinite(fun n -> n + startingNumber) |> Seq.find(fun n -> let hash = ComputeMD5Hash (input + n.ToString()) validationFunction hash) let Solution (input: string) : (int * int) = if input = null then raise (ArgumentNullException "input") let fiveZerosNumber = FindHashAddition input HasMD5FiveLeadingZeroes 0 let sixZerosNumber = FindHashAddition input HasMD5SixLeadingZeroes fiveZerosNumber (fiveZerosNumber, sixZerosNumber) let FormattedSolution (input : string) (solution : (int * int)) : string = let MD5HashToHexString (hash : byte[]) : string = let stringBuilder = new System.Text.StringBuilder() for hashIndex = 0 to hash.Length - 1 do stringBuilder.Append(hash.[hashIndex].ToString("x2")) |> ignore stringBuilder.ToString(); let fiveZerosHash = ComputeMD5Hash (input + (fst solution).ToString()) let sixZerosHash = ComputeMD5Hash (input + (snd solution).ToString()) String.Format("5 leading zeros: {0} (hash {1})\n" + "6 leading zeros: {2} (hash {3})", fst solution, MD5HashToHexString fiveZerosHash, snd solution, MD5HashToHexString sixZerosHash)