private static void MarkPath(PathVisited[,] visitedGrid, int totalSteps, int[,] distanceGrid, int[,] otherDistanceGrid, PathVisited path, PathVisited otherPath, int x, int y) { if (distanceGrid[x, y] == 0) { distanceGrid[x, y] = totalSteps; } if (visitedGrid[x, y] == otherPath) { visitedGrid[x, y] = PathVisited.BOTH; var start = GRID_SIZE / 3; var manhattanDistance = Math.Abs(x - start) + Math.Abs(y - start); Console.WriteLine("Intersection at " + x + ", " + y + " - Manhattan Distance " + manhattanDistance + ", Total Steps " + (distanceGrid[x, y] + otherDistanceGrid[x, y])); } else { visitedGrid[x, y] = path; } }
private void TraversePath(string[] commands, PathVisited[,] visitedGrid, int[,] distanceGrid, int[,] otherDistanceGrid, PathVisited path, PathVisited otherPath) { int x = GRID_SIZE / 3; int y = GRID_SIZE / 3; int totalSteps = 0; foreach (var command in commands) { var direction = command[0]; var distance = int.Parse(command.Substring(1)); switch (direction) { case 'L': for (var i = 0; i < distance; i++) { x -= 1; totalSteps++; MarkPath(visitedGrid, totalSteps, distanceGrid, otherDistanceGrid, path, otherPath, x, y); } continue; case 'R': for (var i = 0; i < distance; i++) { x += 1; totalSteps++; MarkPath(visitedGrid, totalSteps, distanceGrid, otherDistanceGrid, path, otherPath, x, y); } continue; case 'U': for (var i = 0; i < distance; i++) { y += 1; totalSteps++; MarkPath(visitedGrid, totalSteps, distanceGrid, otherDistanceGrid, path, otherPath, x, y); } continue; case 'D': for (var i = 0; i < distance; i++) { y -= 1; totalSteps++; MarkPath(visitedGrid, totalSteps, distanceGrid, otherDistanceGrid, path, otherPath, x, y); } continue; } } }