mirror of
https://github.com/jcreek/advent-of-code.git
synced 2026-07-13 11:13:47 +00:00
feat(2015-04): Add day 4 solution
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
bgvyzdsv
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace AOC.Tests.Y2015
|
||||||
|
{
|
||||||
|
[TestFixture, Parallelizable(ParallelScope.All)]
|
||||||
|
public class Day04
|
||||||
|
{
|
||||||
|
protected string GetThisClassName() { return this.GetType().Name; }
|
||||||
|
private string realData;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2015", "Data", $"{GetThisClassName()}.dat"))[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private int CreateMD5Hash(string input, string startingString)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
MD5 md5 = MD5.Create();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
byte[] inputBytes = new UTF8Encoding().GetBytes($"{input}{result}");
|
||||||
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
foreach (var t in hashBytes)
|
||||||
|
{
|
||||||
|
sb.Append(t.ToString("X2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sb.ToString().StartsWith(startingString))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("abcdef", 609043)]
|
||||||
|
[TestCase("pqrstuv", 1048970)]
|
||||||
|
[TestCase(null, 254575)] // The actual answer
|
||||||
|
public void Part1(string input, int? expected)
|
||||||
|
{
|
||||||
|
string lines = input != null ? input : realData;
|
||||||
|
|
||||||
|
var i = CreateMD5Hash(lines, "00000");
|
||||||
|
|
||||||
|
if (expected != null)
|
||||||
|
{
|
||||||
|
Assert.That(i, Is.EqualTo(expected.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Part 1: {i}");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(null, 1038736)] // The actual answer
|
||||||
|
public void Part2(string input, int? expected)
|
||||||
|
{
|
||||||
|
string lines = input != null ? input : realData;
|
||||||
|
|
||||||
|
var i = CreateMD5Hash(lines, "000000");
|
||||||
|
|
||||||
|
if (expected != null)
|
||||||
|
{
|
||||||
|
Assert.That(i, Is.EqualTo(expected.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Part 2: {i}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
--- Day 4: The Ideal Stocking Stuffer ---
|
||||||
|
Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.
|
||||||
|
|
||||||
|
To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so.
|
||||||
|
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
|
||||||
|
|
||||||
Reference in New Issue
Block a user