Refactor Day07 to extract common coordinate enqueue logic
This commit is contained in:
+8
-6
@@ -59,19 +59,21 @@ public class Day07 : IPuzzleSolver<long>
|
|||||||
if (grid[current.x][current.y] == '^')
|
if (grid[current.x][current.y] == '^')
|
||||||
{
|
{
|
||||||
splittersHit++;
|
splittersHit++;
|
||||||
var nextLeft = (current.x, current.y - 1);
|
CheckAndEnqueueIfNotVisited((current.x, current.y - 1));
|
||||||
var nextRight = (current.x, current.y + 1);
|
CheckAndEnqueueIfNotVisited((current.x, current.y + 1));
|
||||||
if (visited.Add(nextLeft)) seen.Enqueue(nextLeft);
|
|
||||||
if (visited.Add(nextRight)) seen.Enqueue(nextRight);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var next = (current.x + 1, current.y);
|
CheckAndEnqueueIfNotVisited((current.x + 1, current.y));
|
||||||
if (visited.Add(next)) seen.Enqueue(next);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return splittersHit;
|
return splittersHit;
|
||||||
|
|
||||||
|
void CheckAndEnqueueIfNotVisited(Coordinate coordinate)
|
||||||
|
{
|
||||||
|
if (visited.Add(coordinate)) seen.Enqueue(coordinate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<Coordinate, long> GetNodesTraversedWitTimelineCount(string[] grid, Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
|
private Dictionary<Coordinate, long> GetNodesTraversedWitTimelineCount(string[] grid, Coordinate current, HashSet<Coordinate> path, Dictionary<Coordinate, long> nodeCountVisited)
|
||||||
|
|||||||
Reference in New Issue
Block a user