Search code examples
c#functionintersectiondirections

Check if N sections intersect


folks! I need to implement the function static bool CheckSectionsIntersect that checks if the sections intersect (right → ; left ←; down ↓; up ↑). Given a list of N consecutive vertical and horizontal sections of fixed dimensions in a form of a succession of directions. I have to think that I would have a route of N sections. The function should return True if I get to a point I've been to before. For example:

N = 6: { up, left, down, down, right, up} - return True.

⬇⬅
⬇⬆  <- Start
➡⬆

N = 4: {down, left, up, left} - return False.

⬅⬇  <- Start
⬆⬅

The code I wrote, but is incomplete because I need some suggestion to how should be the function:

    static void Main()
    {
        string userInput = Console.ReadLine();
        int numberOfSections = Convert.ToInt32(userInput);
        string[] sectionDirection = new string[numberOfSections];
        for (int i = 0; i < numberOfSections; i++)
        {
            sectionDirections[i] = Console.ReadLine();
        }

        Console.WriteLine(CheckSectionsIntersect(sectionDirection, numberOfSections));
    }

    static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
    {
        return true; // I need an implementation here
    }
}

}

May I have any suggestion for this implementation, please? Thank you very much!


Solution

  • I'm not sure I understand what you are saying. If you want to see if you end in a place where you have been, just make an array of tuples. In each move save your coordinates (starting form 0,0). At the end of the path check if your coordinates are in the array. Or at the end of each move that if you want to check for places where you have already been.

    Edit:

    static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
        {
            (int X, int Y) pos = (0, 0); //Variable to track our actual position. Start position (0,0)
            List<(int X, int Y)> coordinates = new List<(int X, int Y)>(); //List where we are going to keep the coordinates
            foreach (string move in sectionDirection) //for each move
            {
                coordinates.Add(pos); //Add our current position to the list
                switch (move)         //Make a move
                {
                    case "left":
                        pos.X -= 1;
                        break;
                    case "right":
                        pos.X += 1;
                        break;
                    case "down":
                        pos.Y -= 1;
                        break;
                    case "up":
                        pos.Y += 1;
                        break;
                }
                if (coordinates.Contains(pos)) { return true; } //If our new position is already in the list, we have been there.
            }
            return false;
        }
    

    I have tested it and it works. For this purpose it is easier use a list than an array. This is due the methods .Add() an .contains().

    This code is not the strongest one (for example it doesn't accept Upper letters). But since you are a beginner it should be enough. I encourage you to improve it to keep learning.