diff --git a/AdventOfCode/Calendar/2023/Day02/Solution.cs b/AdventOfCode/Calendar/2023/Day02/Solution.cs index ab60c82..804dda8 100644 --- a/AdventOfCode/Calendar/2023/Day02/Solution.cs +++ b/AdventOfCode/Calendar/2023/Day02/Solution.cs @@ -2,9 +2,9 @@ namespace AdventOfCode.Calendar._2023.Day02; -public class Solution : BaseSolution +internal class Solution : BaseSolution { - private readonly SetOfCubes Constraint = new() + private readonly SetOfCubes _constraint = new() { Red = 12, Green = 13, @@ -47,9 +47,9 @@ private async Task SumGameIds() { var sets = SetsInGame(lines[gameId]); - if (sets.TrueForAll(x => x.Red <= Constraint.Red && - x.Green <= Constraint.Green && - x.Blue <= Constraint.Blue)) + if (sets.TrueForAll(x => x.Red <= _constraint.Red && + x.Green <= _constraint.Green && + x.Blue <= _constraint.Blue)) { sum += gameId + 1; } diff --git a/AdventOfCode/Calendar/2023/Day02/SolutionTests.cs b/AdventOfCode/Calendar/2023/Day02/SolutionTests.cs index c6eea76..7c7b7d2 100644 --- a/AdventOfCode/Calendar/2023/Day02/SolutionTests.cs +++ b/AdventOfCode/Calendar/2023/Day02/SolutionTests.cs @@ -9,7 +9,7 @@ private class TestSolution(string[] inputLines) : Solution public override Task ReadInput() => Task.FromResult(inputLines); } - private readonly string[] Input = + private readonly string[] _input = [ "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue", @@ -23,7 +23,7 @@ private class TestSolution(string[] inputLines) : Solution [InlineData(RunMode.PartTwo, 2286)] public async Task SampleRecord(RunMode runMode, int expected) { - var solution = new TestSolution(Input); + var solution = new TestSolution(_input); Assert.Equal(expected, await solution.Run(runMode)); } } diff --git a/AdventOfCode/Calendar/2023/Day03/Solution.cs b/AdventOfCode/Calendar/2023/Day03/Solution.cs index 93e9af1..f786f65 100644 --- a/AdventOfCode/Calendar/2023/Day03/Solution.cs +++ b/AdventOfCode/Calendar/2023/Day03/Solution.cs @@ -11,12 +11,12 @@ namespace AdventOfCode.Calendar._2023.Day03; /// than implementing algorithms from scratch [poorly] -- this really showed /// for part 2. /// -public partial class Solution : BaseSolution +internal partial class Solution : BaseSolution { private const char PeriodSymbol = '.'; private const char GearSymbol = '*'; - private string[] lines = []; + private string[] _lines = []; public override async Task Run(RunMode runMode) { @@ -30,14 +30,14 @@ public override async Task Run(RunMode runMode) private async Task SumPartNumbers() { - lines = await ReadInput(); + _lines = await ReadInput(); var sum = 0; - for (var lineNumber = 0; lineNumber < lines.Length; lineNumber++) + for (var lineNumber = 0; lineNumber < _lines.Length; lineNumber++) { // Draw a bounding box around every number in the row to look // for a symbol. - foreach (var match in NumberRegex().Matches(lines[lineNumber]).Cast()) + foreach (var match in NumberRegex().Matches(_lines[lineNumber]).Cast()) { var boundingBox = GetBoundingBox(lineNumber, match.Index, match.Value); @@ -55,27 +55,27 @@ private async Task SumPartNumbers() private async Task SumGearRatios() { - lines = await ReadInput(); + _lines = await ReadInput(); var sum = 0; - for (var lineNumber = 0; lineNumber < lines.Length; lineNumber++) + for (var lineNumber = 0; lineNumber < _lines.Length; lineNumber++) { // Keep track of adjacent part numbers on the line to avoid // duplicates. var seen = new List>(); - foreach (var match in NumberRegex().Matches(lines[lineNumber]).Cast()) + foreach (var match in NumberRegex().Matches(_lines[lineNumber]).Cast()) { var boundingBox = GetBoundingBox(lineNumber, match.Index, match.Value); // Gear to the left, first character to the left of that isn't // a period, and this number wasn't previously seen. if (boundingBox[(int)Direction.Left].Contains(GearSymbol) && - lines[lineNumber][match.Index - 2] != PeriodSymbol && + _lines[lineNumber][match.Index - 2] != PeriodSymbol && !seen.Exists(x => x.Value == match.Value)) { var rl = GearRatioRLRegex(); - var rlMatches = rl.Match(lines[lineNumber], match.Index); + var rlMatches = rl.Match(_lines[lineNumber], match.Index); if (rlMatches.Success) { @@ -87,10 +87,10 @@ private async Task SumGearRatios() // Gear to the right and the first character next to that // isn't a period. if (boundingBox[(int)Direction.Right].Contains(GearSymbol) && - lines[lineNumber][match.Index + match.Value.Length + 1] != PeriodSymbol) + _lines[lineNumber][match.Index + match.Value.Length + 1] != PeriodSymbol) { var lr = GearRatioLRRegex(); - var rlMatches = lr.Match(lines[lineNumber], match.Index + match.Value.Length); + var rlMatches = lr.Match(_lines[lineNumber], match.Index + match.Value.Length); if (rlMatches.Success) { @@ -102,9 +102,9 @@ private async Task SumGearRatios() var gearPosition = boundingBox[(int)Direction.Bottom].IndexOf(GearSymbol); if (gearPosition != -1 && - lineNumber + 2 < lines.Length && - match.Index + gearPosition < lines[lineNumber + 2].Length && - lines[lineNumber + 2][match.Index + gearPosition] != PeriodSymbol) + lineNumber + 2 < _lines.Length && + match.Index + gearPosition < _lines[lineNumber + 2].Length && + _lines[lineNumber + 2][match.Index + gearPosition] != PeriodSymbol) { // Starting at the position of the gear, find all numbers // to the right and left of it then concatenate them to @@ -112,8 +112,8 @@ private async Task SumGearRatios() var lr = GearRatioLRRegex(); var rl = GearRatioRLRegex(); - var lrMatches = lr.Match(lines[lineNumber + 2], match.Index + gearPosition); - var rlMatches = rl.Match(lines[lineNumber + 2], match.Index + gearPosition); + var lrMatches = lr.Match(_lines[lineNumber + 2], match.Index + gearPosition); + var rlMatches = rl.Match(_lines[lineNumber + 2], match.Index + gearPosition); var partNumber = (rlMatches.Success ? rlMatches.Value : string.Empty) + (lrMatches.Success ? lrMatches.Value : string.Empty); @@ -157,13 +157,13 @@ private string[] GetBoundingBox(int currentLine, int matchIndex, string number) // Add left if (!isFlushLeft) { - boundingBox[(int)Direction.Left] = lines[currentLine][matchIndex - 1].ToString(); + boundingBox[(int)Direction.Left] = _lines[currentLine][matchIndex - 1].ToString(); } // Add right - if (matchIndex + number.Length < lines[currentLine].Length) + if (matchIndex + number.Length < _lines[currentLine].Length) { - boundingBox[(int)Direction.Right] = lines[currentLine][matchIndex + number.Length].ToString(); + boundingBox[(int)Direction.Right] = _lines[currentLine][matchIndex + number.Length].ToString(); } var startingPos = isFlushLeft ? 0 : matchIndex - 1; @@ -171,24 +171,24 @@ private string[] GetBoundingBox(int currentLine, int matchIndex, string number) if (isFlushLeft) { - endPadding = startingPos + number.Length + 1 > lines[currentLine].Length ? 0 : 1; + endPadding = startingPos + number.Length + 1 > _lines[currentLine].Length ? 0 : 1; } else { - endPadding = startingPos + number.Length + 2 > lines[currentLine].Length ? 1 : 2; + endPadding = startingPos + number.Length + 2 > _lines[currentLine].Length ? 1 : 2; } // Add top if (currentLine > 0) { - boundingBox[(int)Direction.Top] = lines[currentLine - 1] + boundingBox[(int)Direction.Top] = _lines[currentLine - 1] .Substring(startingPos, number.Length + endPadding); } // Add bottom - if (currentLine < lines.Length - 1) + if (currentLine < _lines.Length - 1) { - boundingBox[(int)Direction.Bottom] = lines[currentLine + 1] + boundingBox[(int)Direction.Bottom] = _lines[currentLine + 1] .Substring(startingPos, number.Length + endPadding); } @@ -201,7 +201,6 @@ private string[] GetBoundingBox(int currentLine, int matchIndex, string number) [GeneratedRegex(@"\d+")] private static partial Regex GearRatioLRRegex(); - //[GeneratedRegex(@"^(?!\.)\d+", RegexOptions.RightToLeft)] [GeneratedRegex(@"\d+", RegexOptions.RightToLeft)] private static partial Regex GearRatioRLRegex(); }