Refactor Day04 to use Index() extension method

This commit is contained in:
Sebastian Lindemeier 2025-12-05 09:04:27 +01:00
parent 0d028c4282
commit 31caf429ca
2 changed files with 22 additions and 5 deletions

View File

@ -4,7 +4,7 @@ namespace AoC_2025.Tests;
public class Day05Test public class Day05Test
{ {
private readonly IPuzzleSolver<long> _sut; private readonly Day05 _sut;
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day05.txt"; private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day05.txt";
private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day05.txt"; private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day05.txt";
@ -44,4 +44,20 @@ public class Day05Test
Assert.Equal(369761800782619, actual); Assert.Equal(369761800782619, actual);
} }
[Fact]
public void Part02Again_Test_equals_14()
{
var actual = _sut.SolvePart2Again(TestInputPath);
Assert.Equal(14, actual);
}
[Fact]
public void Part02Again_Prod_equals_369761800782619()
{
var actual = _sut.SolvePart2Again(ProdInputPath);
Assert.Equal(369761800782619, actual);
}
} }

View File

@ -55,16 +55,17 @@ public class Day04 : IPuzzleSolver<long>
private HashSet<Coordinate> GetRollCoordinates(IEnumerable<string> paperrollGrid) private HashSet<Coordinate> GetRollCoordinates(IEnumerable<string> paperrollGrid)
{ {
var coordinates = paperrollGrid var coordinates = paperrollGrid
.SelectMany((gridLine, xIndex) => GetCoordinates(gridLine, xIndex)) .Index()
.SelectMany(indexedLine => GetCoordinates(indexedLine.Item, indexedLine.Index))
.ToHashSet(); .ToHashSet();
return coordinates; return coordinates;
} }
private IEnumerable<Coordinate> GetCoordinates(string gridLine, int xIndex) private IEnumerable<Coordinate> GetCoordinates(string gridLine, int xIndex)
{ {
var indexedCharacters = gridLine.Select((character, yIndex) => (character, yIndex)); var indexedCharacters = gridLine.Index();
var validRolls = indexedCharacters.Where(tuple => tuple.character == '@'); var validRolls = indexedCharacters.Where(tuple => tuple.Item == '@');
var rollCoordinates = validRolls.Select(tuple => (xIndex, tuple.yIndex)); var rollCoordinates = validRolls.Select(tuple => (xIndex, tuple.Index));
return rollCoordinates; return rollCoordinates;
} }