Extract Coordinate, IntegerRange and Coordinate3d to new AdventOfCode.HelperClasses project and refactor solution for modularity and type consistency

This commit is contained in:
Sebastian Lindemeier
2025-12-12 13:14:17 +01:00
parent 85516cd783
commit aaf32260d0
13 changed files with 119 additions and 143 deletions
+14 -18
View File
@@ -1,9 +1,5 @@
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using AdvenOfCode.Contracts;
using Coordinate = (int x, int y);
using AdvenOfCode.Contracts;
using AdventOfCode.HelperClasses;
namespace AoC_2025;
@@ -42,7 +38,7 @@ public class Day07 : IPuzzleSolver<long>
private Coordinate GetStart(string[] grid)
{
return (0, grid[0].IndexOf('S'));
return new Coordinate(0, grid[0].IndexOf('S'));
}
private long TraverseGridAndCountSplitters(string[] grid, Coordinate start)
@@ -53,18 +49,18 @@ public class Day07 : IPuzzleSolver<long>
var splittersHit = 0L;
while (seen.TryDequeue(out var current))
{
if (current.x >= grid.Length) continue;
if (current.y >= grid[0].Length || current.y < 0) continue;
if (current.X >= grid.Length) continue;
if (current.Y >= grid[0].Length || current.Y < 0) continue;
if (grid[current.x][current.y] == '^')
if (grid[(int)current.X][(int)current.Y] == '^')
{
splittersHit++;
CheckAndEnqueueIfNotVisited((current.x, current.y - 1));
CheckAndEnqueueIfNotVisited((current.x, current.y + 1));
CheckAndEnqueueIfNotVisited(current with {Y = current.Y - 1});
CheckAndEnqueueIfNotVisited(current with {Y = current.Y + 1});
}
else
{
CheckAndEnqueueIfNotVisited((current.x + 1, current.y));
CheckAndEnqueueIfNotVisited(current with {X = current.X + 1});
}
}
@@ -79,21 +75,21 @@ public class Day07 : IPuzzleSolver<long>
// Dictionary only for memoization
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> memory)
{
if (current.x >= grid.Length)
if (current.X >= grid.Length)
{
return 1;
}
if(memory.TryGetValue(current, out var count)) return count;
if (grid[current.x][current.y] == '^')
if (grid[(int)current.X][(int)current.Y] == '^')
{
var resLeft = GetTimelinesCountRecursive(grid, (current.x, current.y - 1), memory);
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), memory);
var resLeft = GetTimelinesCountRecursive(grid, current with {Y = current.Y - 1}, memory);
var resRight = GetTimelinesCountRecursive(grid, current with {Y = current.Y + 1}, memory);
memory[current] = resLeft + resRight;
return resLeft + resRight;
}
var res = GetTimelinesCountRecursive(grid, (current.x + 1, current.y), memory);
var res = GetTimelinesCountRecursive(grid, current with {X = current.X + 1}, memory);
memory[current] = res;
return res;
}