From ade1148cc060bb73ce7c0915bad7a4a1794dd6fe Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Fri, 11 Nov 2022 16:31:34 -0500 Subject: [PATCH] 2015 day 4 --- AOC.Test/Test2015.cs | 2 ++ AOC2015/Day04.cs | 35 ++++++++++++++++++++++++++++++++--- AOC2015/input2015/test04.in | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 AOC2015/input2015/test04.in diff --git a/AOC.Test/Test2015.cs b/AOC.Test/Test2015.cs index 28c2fe3..08adb01 100644 --- a/AOC.Test/Test2015.cs +++ b/AOC.Test/Test2015.cs @@ -9,6 +9,7 @@ public class Test2015 [DataRow(typeof(Day01), "232", "1783")] [DataRow(typeof(Day02),"1586300", "3737498")] [DataRow(typeof(Day03), "2081", "2341")] + // [DataRow(typeof(Day04), "346386", "9958218")] public void TestAllDays(Type dayType, string part1, string part2) { Common.CheckDay(dayType, part1, part2); @@ -18,6 +19,7 @@ public class Test2015 [DataRow(typeof(Day01), "-1", "5")] [DataRow(typeof(Day02), "58", "34")] [DataRow(typeof(Day03), "2", "11")] + // [DataRow(typeof(Day04), "609043", "6742839")] public void CheckTestInputs(Type dayType, string part1, string part2) { Day.UseTestInput = true; diff --git a/AOC2015/Day04.cs b/AOC2015/Day04.cs index ad56125..2fa2cdd 100644 --- a/AOC2015/Day04.cs +++ b/AOC2015/Day04.cs @@ -1,15 +1,44 @@ -namespace AOC2015; +using System.Security.Cryptography; + +namespace AOC2015; /// /// Day 4: /// public sealed class Day04 : Day { + private readonly string _key; + public Day04() : base(2015, 4, "Puzzle Name") { + _key = Input.First(); } - public override object Part1() => ""; + public override object Part1() + { + var md5 = MD5.Create(); + var counter = 0; - public override object Part2() => ""; + while (true) + { + var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(_key + counter)); + if (BitConverter.ToString(hash).Replace("-", "").StartsWith("00000")) + return counter; + counter++; + } + } + + public override object Part2() + { + var md5 = MD5.Create(); + var counter = 0; + + while (true) + { + var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(_key + counter)); + if (BitConverter.ToString(hash).Replace("-", "").StartsWith("000000")) + return counter; + counter++; + } + } } diff --git a/AOC2015/input2015/test04.in b/AOC2015/input2015/test04.in new file mode 100644 index 0000000..0373d93 --- /dev/null +++ b/AOC2015/input2015/test04.in @@ -0,0 +1 @@ +abcdef