diff --git a/AoC_2026.Tests/Day05Test.cs b/AoC_2026.Tests/Day05Test.cs new file mode 100644 index 0000000..00a876d --- /dev/null +++ b/AoC_2026.Tests/Day05Test.cs @@ -0,0 +1,47 @@ +using AdvenOfCode.Contracts; + +namespace AoC_2026.Tests; + +public class Day05Test +{ + private readonly IPuzzleSolver _sut; + private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day05.txt"; + private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day05.txt"; + + public Day05Test() + { + _sut = new Day05(); + } + [Fact] + public void Part01_Test_equals_4268() + { + var actual = _sut.SolvePart1(TestInputPath); + + Assert.Equal(4268, actual); + } + + [Fact] + public void Part01_Prod_equals_18957() + { + var actual = _sut.SolvePart1(ProdInputPath); + + Assert.Equal(18957, actual); + } + + [Fact] + public void Part02_Test_equals_35332647() + { + var actual = _sut.SolvePart2(TestInputPath); + + Assert.Equal(35332647, actual); + } + + [Fact] + public void Part02_Prod_equals_40767683() + { + var actual = _sut.SolvePart2(ProdInputPath); + + Assert.Equal(40767683, actual); + } +} \ No newline at end of file diff --git a/AoC_2026/Day05.cs b/AoC_2026/Day05.cs new file mode 100644 index 0000000..6202a3a --- /dev/null +++ b/AoC_2026/Day05.cs @@ -0,0 +1,129 @@ +using AdvenOfCode.Contracts; +using AdventOfCode.Extensions; + +namespace AoC_2026; + +public class Day05 : IPuzzleSolver +{ + private static Func[] ParsePuzzleInput(string path) + { + var puzzleInput = File.ReadAllText(path) + .Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Select(btn => ParseButton(btn)) + .ToArray(); + return puzzleInput; + } + + private static Func ParseButton(string button) + { + button = button[1..^1]; + if(button == "bksp") + return Subtract; + if (button == "oct") + return ToOctal; + if (button.StartsWith('*')) + { + var a = int.Parse(button[1..]); + return display => Multiply(display, a); + } + if (button.StartsWith('/')) + { + var a = int.Parse(button[1..]); + return display => Divide(display, a); + } + if (button.IndexOf("->", StringComparison.Ordinal) > 0) + { + var parts = button.Split("->"); + var a = parts[0]; + var b = parts[1]; + return display => Replace(display, a, b); + } + else + { + var a = int.Parse(button); + return display => Add(display, a); + } + } + + public long SolvePart1(string pathToPuzzleInput) + { + var buttons = ParsePuzzleInput(pathToPuzzleInput); + var uniqeNumbersAtDepth = GetUniqulyAchievableNumbers(0, buttons, [], 3); + var result = uniqeNumbersAtDepth + .Select(x =>(long)x.number) + .Distinct() + .Sum(); + return result; + + } + + public long SolvePart2(string pathToPuzzleInput) + { + var buttons = ParsePuzzleInput(pathToPuzzleInput); + var uniqeNumbersAtDepth = GetUniqulyAchievableNumbers(0, buttons, []); + var result = uniqeNumbersAtDepth + .Select(x =>(long)x.number) + .Distinct() + .Sum(); + return result; + } + + private static HashSet<(int number, int depth)> GetUniqulyAchievableNumbers(int display, Func[] buttons, + HashSet<(int number, int depth)> seenDisplayValues, int maxPresses = -1) + { + if (!seenDisplayValues.Add((display, maxPresses)) || maxPresses == 0) + return seenDisplayValues; + + foreach (var button in buttons) + { + GetUniqulyAchievableNumbers(ApplyDisplaySafety(button(display), display), buttons, seenDisplayValues, Math.Max(-1, maxPresses - 1)); + } + + return seenDisplayValues; + } + + private static int ApplyDisplaySafety(int display, int displayOld) + { + return display > 9999 ? displayOld : display; + } + + private static int Add(int display, int buttonValue) + { + return display * 10 + buttonValue; + } + + private static int Multiply(int display, int buttonValue) + { + return display * buttonValue; + } + + private static int Divide(int display, int buttonValue) + { + return display / buttonValue; + } + + private static int Subtract(int display) + { + return display / 10; + } + + private static int ToOctal(int display) + { + var nextDisplay = 0; + var i = 1; + + while (display > 0) + { + nextDisplay += i * (display % 8); + display /= 8; + i *= 10; + } + + return nextDisplay; + } + + private static int Replace(int display, string aValue, string bValue) + { + return int.Parse(display.ToString().Replace(aValue, bValue)); + } +} \ No newline at end of file