public void ParseTwoRovers()
        {
            var lines = new[]
            {
                "10 12",
                "5 6 N",
                "M",
                "7 8 E",
                "L"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsTrue(result.Ok);
            Assert.AreEqual((10, 12), (result.MaxX, result.MaxY));

            Assert.AreEqual(2, result.RoverSpecs.Length);
            var roverSpecA = result.RoverSpecs[0];
            var roverSpecB = result.RoverSpecs[1];

            Assert.AreEqual(
                (5, 6, CompassDirection.North, RoverInstruction.Move),
                (roverSpecA.X, roverSpecA.Y, roverSpecA.Facing, roverSpecA.Instructions.Single()));

            Assert.AreEqual(
                (7, 8, CompassDirection.East, RoverInstruction.TurnLeft),
                (roverSpecB.X, roverSpecB.Y, roverSpecB.Facing, roverSpecB.Instructions.Single()));
        }
        public void ParseSingleRover()
        {
            var lines = new[]
            {
                "10 12",
                "5 6 N",
                "MMLR"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsTrue(result.Ok);
            Assert.AreEqual((10, 12), (result.MaxX, result.MaxY));

            Assert.AreEqual(1, result.RoverSpecs.Length);
            var roverSpec = result.RoverSpecs[0];

            Assert.AreEqual((5, 6, CompassDirection.North), (roverSpec.X, roverSpec.Y, roverSpec.Facing));
            Assert.AreEqual(
                new[]
            {
                RoverInstruction.Move,
                RoverInstruction.Move,
                RoverInstruction.TurnLeft,
                RoverInstruction.TurnRight
            },
                roverSpec.Instructions);
        }
        public void RejectsBadHeader(string line, string expectedError)
        {
            var lines = new[]
            {
                line
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsFalse(result.Ok);
            Assert.AreEqual(expectedError, result.ParseError);
        }
        public void AllCornersAreInBounds(int x, int y)
        {
            var lines = new[]
            {
                "10 12",
                $"{x} {y} N",
                "M"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsTrue(result.Ok);
        }
        public void RejectsEvenLineCount()
        {
            var lines = new[]
            {
                "5 5",
                "1 1 N"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsFalse(result.Ok);
            Assert.AreEqual("Expected odd number of lines", result.ParseError);
        }
        public void RejectsBadInstruction()
        {
            var lines = new[]
            {
                "10 10",
                "5 5 N",
                "MxLR"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsFalse(result.Ok);
            Assert.AreEqual("Unexpected instruction on line: MxLR", result.ParseError);
        }
        public void RejectsOutOfBounds(int x, int y)
        {
            var lines = new[]
            {
                "10 12",
                $"{x} {y} N",
                "M"
            };

            var result = RoverSimInputParser.Parse(lines);

            Assert.IsFalse(result.Ok);
            Assert.AreEqual($"Rover at {x}, {y} is out of bounds", result.ParseError);
        }
Example #8
0
        static void Main(string[] args)
        {
            //TODO - read this from file path specified in args?
            string[] lines = new[]
            {
                "5 5",
                "1 2 N",
                "LMLMLMLMM",
                "3 3 E",
                "MMRMMRMRRM"
            };

            var parseResult = RoverSimInputParser.Parse(lines);

            if (!parseResult.Ok)
            {
                Console.Out.WriteLine("Parse error: " + parseResult.ParseError);
                Environment.Exit(0);
            }

            var rovers = ProcessRovers(parseResult);

            OutputRovers(rovers);
        }