Fix Day05 display operations to use long
This commit is contained in:
1 parent
5228c9e6a2
commit
ba67ba7dde
1 file changed
+21
-39
+21
-39
@@ -5,7 +5,7 @@ namespace AoC_2026;
|
||||
|
||||
public class Day05 : IPuzzleSolver<long>
|
||||
{
|
||||
private static Func<int, int>[] ParsePuzzleInput(string path)
|
||||
private static Func<long, long>[] ParsePuzzleInput(string path)
|
||||
{
|
||||
var puzzleInput = File.ReadAllText(path)
|
||||
.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
@@ -14,35 +14,17 @@ public class Day05 : IPuzzleSolver<long>
|
||||
return puzzleInput;
|
||||
}
|
||||
|
||||
private static Func<int, int> ParseButton(string button)
|
||||
private static Func<long, long> ParseButton(string button)
|
||||
{
|
||||
button = button[1..^1];
|
||||
if(button == "bksp")
|
||||
return Subtract;
|
||||
if (button == "oct")
|
||||
return ToOctal;
|
||||
if (button.StartsWith('*'))
|
||||
return button[1..^1] switch
|
||||
{
|
||||
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);
|
||||
}
|
||||
"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)
|
||||
@@ -68,8 +50,8 @@ public class Day05 : IPuzzleSolver<long>
|
||||
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)
|
||||
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;
|
||||
@@ -82,34 +64,34 @@ public class Day05 : IPuzzleSolver<long>
|
||||
return seenDisplayValues;
|
||||
}
|
||||
|
||||
private static int ApplyDisplaySafety(int display, int displayOld)
|
||||
private static long ApplyDisplaySafety(long display, long displayOld)
|
||||
{
|
||||
return display > 9999 ? displayOld : display;
|
||||
}
|
||||
|
||||
private static int Add(int display, int buttonValue)
|
||||
private static long Add(long display, long buttonValue)
|
||||
{
|
||||
return display * 10 + buttonValue;
|
||||
}
|
||||
|
||||
private static int Multiply(int display, int buttonValue)
|
||||
private static long Multiply(long display, long buttonValue)
|
||||
{
|
||||
return display * buttonValue;
|
||||
}
|
||||
|
||||
private static int Divide(int display, int buttonValue)
|
||||
private static long Divide(long display, long buttonValue)
|
||||
{
|
||||
return display / buttonValue;
|
||||
}
|
||||
|
||||
private static int Subtract(int display)
|
||||
private static long Subtract(long display)
|
||||
{
|
||||
return display / 10;
|
||||
}
|
||||
|
||||
private static int ToOctal(int display)
|
||||
private static long ToOctal(long display)
|
||||
{
|
||||
var nextDisplay = 0;
|
||||
var nextDisplay = 0L;
|
||||
var i = 1;
|
||||
|
||||
while (display > 0)
|
||||
@@ -122,8 +104,8 @@ public class Day05 : IPuzzleSolver<long>
|
||||
return nextDisplay;
|
||||
}
|
||||
|
||||
private static int Replace(int display, string aValue, string bValue)
|
||||
private static long Replace(long display, string aValue, string bValue)
|
||||
{
|
||||
return int.Parse(display.ToString().Replace(aValue, bValue));
|
||||
return long.Parse(display.ToString().Replace(aValue, bValue));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user