112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
using AdvenOfCode.Contracts;
|
|
using AdventOfCode.Extensions;
|
|
|
|
namespace AoC_2026;
|
|
|
|
public class Day05 : IPuzzleSolver<long>
|
|
{
|
|
private static Func<long, long>[] ParsePuzzleInput(string path)
|
|
{
|
|
var puzzleInput = File.ReadAllText(path)
|
|
.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.Select(btn => ParseButton(btn))
|
|
.ToArray();
|
|
return puzzleInput;
|
|
}
|
|
|
|
private static Func<long, long> ParseButton(string button)
|
|
{
|
|
return button[1..^1] switch
|
|
{
|
|
"bksp" => Subtract,
|
|
"oct" => ToOctal,
|
|
var btn when btn.StartsWith('*') => display => Multiply(display, long.Parse(btn[1..])),
|
|
var btn when btn.StartsWith('/') => display => Divide(display, long.Parse(btn[1..])),
|
|
var btn when btn.Split("->") is [var a, var b] => display => Replace(display, a, b),
|
|
var btn => display => Add(display, long.Parse(btn))
|
|
};
|
|
}
|
|
|
|
public long SolvePart1(string pathToPuzzleInput)
|
|
{
|
|
var buttons = ParsePuzzleInput(pathToPuzzleInput);
|
|
var uniqeNumbersAtDepth = GetUniqulyAchievableNumbers(0, buttons, [], 3);
|
|
var result = uniqeNumbersAtDepth
|
|
.Select(x => 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 => x.number)
|
|
.Distinct()
|
|
.Sum();
|
|
return result;
|
|
}
|
|
|
|
private static HashSet<(long number, int depth)> GetUniqulyAchievableNumbers(long display, Func<long, long>[] buttons,
|
|
HashSet<(long number, int depth)> seenDisplayValues, int maxPresses = -1)
|
|
{
|
|
if (!seenDisplayValues.Add((display, maxPresses)) || maxPresses == 0)
|
|
return seenDisplayValues;
|
|
|
|
foreach (var button in buttons)
|
|
{
|
|
var nextDisplay = ApplyDisplaySafety(button(display), display);
|
|
seenDisplayValues = GetUniqulyAchievableNumbers(nextDisplay, buttons, seenDisplayValues, Math.Max(-1, maxPresses - 1));
|
|
}
|
|
|
|
return seenDisplayValues;
|
|
}
|
|
|
|
private static long ApplyDisplaySafety(long display, long displayOld)
|
|
{
|
|
return display > 9999 ? displayOld : display;
|
|
}
|
|
|
|
private static long Add(long display, long buttonValue)
|
|
{
|
|
return display * 10 + buttonValue;
|
|
}
|
|
|
|
private static long Multiply(long display, long buttonValue)
|
|
{
|
|
return display * buttonValue;
|
|
}
|
|
|
|
private static long Divide(long display, long buttonValue)
|
|
{
|
|
return display / buttonValue;
|
|
}
|
|
|
|
private static long Subtract(long display)
|
|
{
|
|
return display / 10;
|
|
}
|
|
|
|
private static long ToOctal(long display)
|
|
{
|
|
var nextDisplay = 0L;
|
|
var i = 1;
|
|
|
|
while (display > 0)
|
|
{
|
|
nextDisplay += i * (display % 8);
|
|
display /= 8;
|
|
i *= 10;
|
|
}
|
|
|
|
return nextDisplay;
|
|
}
|
|
|
|
private static long Replace(long display, string aValue, string bValue)
|
|
{
|
|
return long.Parse(display.ToString().Replace(aValue, bValue));
|
|
}
|
|
} |