Skip to content

Commit

Permalink
2018 Day05 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 10, 2023
1 parent 5ea720d commit a685ef3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 25 additions & 10 deletions Solutions/2018/Day05.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,35 @@ namespace AdventOfCode.Solutions._2018;
/// </summary>
[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();
Expand All @@ -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<string> inputs = input.ToList();
return "** Solution not written yet **";
return polymer;
}
}
2 changes: 1 addition & 1 deletion Tests/2018/Tests_05.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a685ef3

Please sign in to comment.