Example #1
0
        static void Main(string[] args)
        {
            string inputFile = AppDomain.CurrentDomain.BaseDirectory + "Input.txt";

            try
            {
                var fileLines = File.ReadLines(inputFile).GetEnumerator();

                // Use the first line to create the map
                fileLines.MoveNext();
                MapGrid map = InputHandler.CreateMap(fileLines.Current);

                MappedRover rover = null;

                while (fileLines.MoveNext())
                {
                    string input = fileLines.Current;

                    if (rover == null)
                    {
                        rover = InputHandler.CreateRover(map, input);
                    }
                    else
                    {
                        string status = InputHandler.CommandRover(rover, input);
                        Console.WriteLine(status);
                        rover = null;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Input file could not be processed: {e.Message}");
            }
        }
Example #2
0
        /// <summary>
        /// Sends the specified commands to the rover, then returns its final
        /// coordinates and direction.
        /// </summary>
        /// <param name="rover">Rover to send commands to</param>
        /// <param name="input">Commands to perform</param>
        public static string CommandRover(MappedRover rover, string input)
        {
            if (rover == null)
            {
                throw new ArgumentNullException(nameof(rover));
            }
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new ArgumentException("Commands not provided", nameof(input));
            }

            foreach (char command in input)
            {
                if (command == 'M')
                {
                    rover.TryMoveForward();
                }
                else if (command == 'L')
                {
                    rover.TurnLeft();
                }
                else if (command == 'R')
                {
                    rover.TurnRight();
                }
            }

            char   directionChar = rover.Direction.ToString()[0];
            string status        = $"{rover.XCoordinate} {rover.YCoordinate} {directionChar}";

            return(status);
        }