Add 2026 Day05 solution and tests
This commit is contained in:
1 parent
120f3ac96c
commit
f158e24247
2 files changed
+176
No files matched your search
@@ -0,0 +1,47 @@
|
|||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
|
||||||
|
namespace AoC_2026.Tests;
|
||||||
|
|
||||||
|
public class Day05Test
|
||||||
|
{
|
||||||
|
private readonly IPuzzleSolver<long> _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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
using AdventOfCode.Extensions;
|
||||||
|
|
||||||
|
namespace AoC_2026;
|
||||||
|
|
||||||
|
public class Day05 : IPuzzleSolver<long>
|
||||||
|
{
|
||||||
|
private static Func<int, int>[] ParsePuzzleInput(string path)
|
||||||
|
{
|
||||||
|
var puzzleInput = File.ReadAllText(path)
|
||||||
|
.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||||
|
.Select(btn => ParseButton(btn))
|
||||||
|
.ToArray();
|
||||||
|
return puzzleInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Func<int, int> 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<int, int>[] 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user