Skip to content

Commit

Permalink
2018 Day05 Part1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 10, 2023
1 parent ac511f2 commit 5ea720d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Data/2018_05.txt

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions Solutions/2018/Day05.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Text;

namespace AdventOfCode.Solutions._2018;

/// <summary>
/// Day 05: Alchemical Reduction
/// https://adventofcode.com/2018/day/05
/// </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) {
string polymer = input[0];

int currentLength;
do {
StringBuilder newPolymer = new();
currentLength = polymer.Length;
for (int i = 0; i < polymer.Length; i++) {
if (i < polymer.Length - 1 && (polymer[i] + OFFSET == polymer[i + 1] || polymer[i] == polymer[i + 1] + OFFSET)) {
i++;
} else {
_ = newPolymer.Append(polymer[i]);
}
}
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 **";
}
}
26 changes: 26 additions & 0 deletions Tests/2018/Tests_05.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace AdventOfCode.Tests.Year2018;

public class Tests_05_Alchemical_Reaction
{
const int DAY = 5;

[Theory]
[InlineData("aA", 0)]
[InlineData("abBA", 0)]
[InlineData("abAB", 4)]
[InlineData("aabAAB", 6)]
[InlineData("dabAcCaCBAcCcaDA", 10)]
public void Part1(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input), out int actual);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("aA", 9999)]
public void Part2(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input), out int actual);
Assert.Equal(expected, actual);
}
}

0 comments on commit 5ea720d

Please sign in to comment.