Adding Day 4 Solution

This commit is contained in:
Brandon Scott 2017-12-11 23:02:15 -06:00
parent fc8ee07ef9
commit fe586fd098
1 changed files with 47 additions and 0 deletions

47
csharp/Day4Solution.cs Normal file
View File

@ -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;
}
}
}