Refactor Day07 to simplify timeline count logic and remove redundant methods
This commit is contained in:
parent
92ef64fd88
commit
14e4a8dfe9
|
|
@ -29,14 +29,14 @@ public class Day07 : IPuzzleSolver<long>
|
||||||
{
|
{
|
||||||
var grid = ParsePuzzleInput(pathToPuzzleInput);
|
var grid = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var start = GetStart(grid);
|
var start = GetStart(grid);
|
||||||
var nodesWithCount = GetNodesTraversedWitTimelineCount(grid, start, [], []);
|
var nodesWithCount = GetTimelinesCountRecursive(grid, start, []);
|
||||||
return nodesWithCount[start];
|
return nodesWithCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long SolvePart2Again(string pathToPuzzleInput)
|
public long SolvePart2Again(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var grid = ParsePuzzleInput(pathToPuzzleInput);
|
var grid = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var nodesWithCount = GetNodesTraversedWitTimelineCount(grid);
|
var nodesWithCount = GetTimelinesCount(grid);
|
||||||
return nodesWithCount.Sum();
|
return nodesWithCount.Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,40 +76,29 @@ public class Day07 : IPuzzleSolver<long>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<Coordinate, long> GetNodesTraversedWitTimelineCount(string[] grid, Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
|
// Dictionary only for memoization
|
||||||
|
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> nodeCountVisited)
|
||||||
{
|
{
|
||||||
if (current.x >= grid.Length || nodeCountVisited.ContainsKey(current))
|
if (current.x >= grid.Length)
|
||||||
{
|
{
|
||||||
nodeCountVisited = UpdateNodeVisitedCounts(current, path, nodeCountVisited);
|
return 1;
|
||||||
return nodeCountVisited;
|
|
||||||
}
|
}
|
||||||
|
if(nodeCountVisited.TryGetValue(current, out var count)) return count;
|
||||||
nodeCountVisited[current] = 0;
|
|
||||||
path.Add(current);
|
|
||||||
|
|
||||||
if (grid[current.x][current.y] == '^')
|
if (grid[current.x][current.y] == '^')
|
||||||
{
|
{
|
||||||
var resLeft = GetNodesTraversedWitTimelineCount(grid, (current.x, current.y - 1), [..path], nodeCountVisited);
|
var resLeft = GetTimelinesCountRecursive(grid, (current.x, current.y - 1), nodeCountVisited);
|
||||||
var resRight = GetNodesTraversedWitTimelineCount(grid, (current.x, current.y + 1), [..path], resLeft);
|
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), nodeCountVisited);
|
||||||
return resRight;
|
nodeCountVisited[current] = resLeft + resRight;
|
||||||
|
return resLeft + resRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
var res = GetNodesTraversedWitTimelineCount(grid, (current.x + 1, current.y), [..path], nodeCountVisited);
|
var res = GetTimelinesCountRecursive(grid, (current.x + 1, current.y), nodeCountVisited);
|
||||||
|
nodeCountVisited[current] = res;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<Coordinate, long> UpdateNodeVisitedCounts(Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
|
private long[] GetTimelinesCount(string[] grid)
|
||||||
{
|
|
||||||
var addAmount = nodeCountVisited.GetValueOrDefault(current, 1);
|
|
||||||
foreach (var node in path)
|
|
||||||
{
|
|
||||||
nodeCountVisited[node] += addAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nodeCountVisited;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long[] GetNodesTraversedWitTimelineCount(string[] grid)
|
|
||||||
{
|
{
|
||||||
var numbersGrid = grid
|
var numbersGrid = grid
|
||||||
.Select(line => line.Select(c => 0L).ToArray())
|
.Select(line => line.Select(c => 0L).ToArray())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue