From fe586fd09863eb469e383fb1162280d8ec66f6aa Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Mon, 11 Dec 2017 23:02:15 -0600 Subject: [PATCH] Adding Day 4 Solution --- csharp/Day4Solution.cs | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 csharp/Day4Solution.cs diff --git a/csharp/Day4Solution.cs b/csharp/Day4Solution.cs new file mode 100644 index 0000000..ce654db --- /dev/null +++ b/csharp/Day4Solution.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace AOC2015 +{ + class Day4Solution : Solution + { + public override int Day => 4; + public override string Name => "Day 4: The Ideal Stocking Stuffer"; + + public override void Run() + { + string input = "yzbqklnj"; // Puzzle Input + + Console.WriteLine($"Part 1 Answer: {Part1(input)}"); + Console.WriteLine($"Part 2 Answer: {Part2(input)}"); + } + + private int Part1(string input) + { + MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider(); + for (int i = 0; i < int.MaxValue; i++) + { + byte[] hashBytes = md5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(input + i)); + string hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); + if (hash.StartsWith("00000")) + return i; + } + return -1; + } + + private int Part2(string input) + { + MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider(); + for (int i = 0; i < int.MaxValue; i++) + { + byte[] hashBytes = md5CryptoServiceProvider.ComputeHash(Encoding.ASCII.GetBytes(input + i)); + string hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); + if (hash.StartsWith("000000")) + return i; + } + return -1; + } + } +}