diff --git a/Solutions/2018/Day05.cs b/Solutions/2018/Day05.cs index 1cac432..cb23363 100644 --- a/Solutions/2018/Day05.cs +++ b/Solutions/2018/Day05.cs @@ -8,14 +8,35 @@ namespace AdventOfCode.Solutions._2018; /// [Description("Alchemical Reduction")] public sealed partial class Day05 { - private const int OFFSET = 'a' - 'A'; - public static string Part1(string[] input, params object[]? _) => Solution1(input).ToString(); public static string Part2(string[] input, params object[]? _) => Solution2(input).ToString(); - private static int Solution1(string[] input) { + private static int Solution1(string[] input) + { + return ReactPolymer(input[0]).Length; + } + + private static int Solution2(string[] input) + { string polymer = input[0]; + int bestLength = int.MaxValue; + + for (int i = 0; i < 26; i++) { + int length = ReactPolymer(polymer + .Replace(Convert.ToChar(i + 'a').ToString(), "") + .Replace(Convert.ToChar(i + 'A').ToString(), "") + ).Length; + if (length < bestLength) { + bestLength = length; + } + } + return bestLength; + } + + private static string ReactPolymer(string polymer) + { + const int OFFSET = 'a' - 'A'; int currentLength; do { StringBuilder newPolymer = new(); @@ -30,12 +51,6 @@ private static int Solution1(string[] input) { polymer = newPolymer.ToString(); } while (polymer.Length != 0 && polymer.Length != currentLength); - return polymer.Length; - } - - private static string Solution2(string[] input) { - //string inputLine = input[0]; - //List inputs = input.ToList(); - return "** Solution not written yet **"; + return polymer; } } diff --git a/Tests/2018/Tests_05.cs b/Tests/2018/Tests_05.cs index 8511406..25d81cb 100644 --- a/Tests/2018/Tests_05.cs +++ b/Tests/2018/Tests_05.cs @@ -17,7 +17,7 @@ public void Part1(string input, int expected) } [Theory] - [InlineData("aA", 9999)] + [InlineData("dabAcCaCBAcCcaDA", 4)] public void Part2(string input, int expected) { _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input), out int actual);