Compare commits

..

No commits in common. "249e092494b0b423356321e2fa02a49b1769b52b" and "92ef64fd887f0c876d19a89e45e4ecb5d45a597e" have entirely different histories.

1 changed files with 27 additions and 16 deletions

View File

@ -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 = GetTimelinesCountRecursive(grid, start, []); var nodesWithCount = GetNodesTraversedWitTimelineCount(grid, start, [], []);
return nodesWithCount; return nodesWithCount[start];
} }
public long SolvePart2Again(string pathToPuzzleInput) public long SolvePart2Again(string pathToPuzzleInput)
{ {
var grid = ParsePuzzleInput(pathToPuzzleInput); var grid = ParsePuzzleInput(pathToPuzzleInput);
var nodesWithCount = GetTimelinesCount(grid); var nodesWithCount = GetNodesTraversedWitTimelineCount(grid);
return nodesWithCount.Sum(); return nodesWithCount.Sum();
} }
@ -76,29 +76,40 @@ public class Day07 : IPuzzleSolver<long>
} }
} }
// Dictionary only for memoization private Dictionary<Coordinate, long> GetNodesTraversedWitTimelineCount(string[] grid, Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
private long GetTimelinesCountRecursive(string[] grid, Coordinate current, Dictionary<Coordinate, long> memory)
{ {
if (current.x >= grid.Length) if (current.x >= grid.Length || nodeCountVisited.ContainsKey(current))
{ {
return 1; nodeCountVisited = UpdateNodeVisitedCounts(current, path, nodeCountVisited);
return nodeCountVisited;
} }
if(memory.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 = GetTimelinesCountRecursive(grid, (current.x, current.y - 1), memory); var resLeft = GetNodesTraversedWitTimelineCount(grid, (current.x, current.y - 1), [..path], nodeCountVisited);
var resRight = GetTimelinesCountRecursive(grid, (current.x, current.y + 1), memory); var resRight = GetNodesTraversedWitTimelineCount(grid, (current.x, current.y + 1), [..path], resLeft);
memory[current] = resLeft + resRight; return resRight;
return resLeft + resRight;
} }
var res = GetTimelinesCountRecursive(grid, (current.x + 1, current.y), memory); var res = GetNodesTraversedWitTimelineCount(grid, (current.x + 1, current.y), [..path], nodeCountVisited);
memory[current] = res;
return res; return res;
} }
private long[] GetTimelinesCount(string[] grid) private static Dictionary<Coordinate, long> UpdateNodeVisitedCounts(Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
{
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())