2022-12-03 05:55:49 +00:00
|
|
|
using System.Security.Cryptography;
|
2022-11-11 21:31:34 +00:00
|
|
|
|
|
|
|
namespace AOC2015;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-12-03 05:41:38 +00:00
|
|
|
/// Day 4: <a href="https://adventofcode.com/2015/day/4"/>
|
2022-11-11 18:42:42 +00:00
|
|
|
/// </summary>
|
|
|
|
public sealed class Day04 : Day
|
|
|
|
{
|
2022-11-11 21:31:34 +00:00
|
|
|
private readonly string _key;
|
|
|
|
|
2022-11-12 19:06:27 +00:00
|
|
|
public Day04() : base(2015, 4, "The Ideal Stocking Stuffer")
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-11-11 21:31:34 +00:00
|
|
|
_key = Input.First();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object Part1()
|
|
|
|
{
|
|
|
|
var md5 = MD5.Create();
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(_key + counter));
|
|
|
|
if (BitConverter.ToString(hash).Replace("-", "").StartsWith("00000"))
|
|
|
|
return counter;
|
|
|
|
counter++;
|
|
|
|
}
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 21:31:34 +00:00
|
|
|
public override object Part2()
|
|
|
|
{
|
|
|
|
var md5 = MD5.Create();
|
|
|
|
var counter = 0;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
2022-11-11 21:31:34 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(_key + counter));
|
|
|
|
if (BitConverter.ToString(hash).Replace("-", "").StartsWith("000000"))
|
|
|
|
return counter;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|