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>
|
2023-09-20 18:38:58 +00:00
|
|
|
public sealed class Day04() : Day(2015, 4, "The Ideal Stocking Stuffer")
|
2022-11-11 18:42:42 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
private string? _key;
|
2022-11-11 21:31:34 +00:00
|
|
|
|
2023-12-01 07:30:47 +00:00
|
|
|
public override void ProcessInput() => _key = Input.First();
|
2022-11-11 21:31:34 +00:00
|
|
|
|
|
|
|
public override object Part1()
|
|
|
|
{
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
var hash = MD5.HashData(Encoding.ASCII.GetBytes(_key + counter));
|
|
|
|
if (BitConverter.ToString(hash).Replace("-", "").StartsWith("00000"))
|
2022-11-11 21:31:34 +00:00
|
|
|
return counter;
|
|
|
|
counter++;
|
|
|
|
}
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 21:31:34 +00:00
|
|
|
public override object Part2()
|
|
|
|
{
|
2023-09-18 18:48:24 +00:00
|
|
|
var counter = 9_000_000;
|
2022-11-11 18:42:42 +00:00
|
|
|
|
2022-11-11 21:31:34 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2023-09-18 18:48:24 +00:00
|
|
|
var hashBytes = MD5.HashData(Encoding.ASCII.GetBytes(_key + counter));
|
|
|
|
if (hashBytes[0] == 0 && hashBytes[1] == 0 && hashBytes[2] == 0)
|
2022-11-11 21:31:34 +00:00
|
|
|
return counter;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 18:42:42 +00:00
|
|
|
}
|