Ejemplo n.º 1
0
        public CoOrdinates TransformInstructionToDistance(string instruction)
        {
            var result    = new CoOrdinates();
            var direction = instruction[0].ToString();
            var distance  = Int32.Parse(instruction.Substring(1));

            switch (direction)
            {
            case "R":
                result.X = distance;
                break;

            case "U":
                result.Y = distance;
                break;

            case "L":
                result.X = Math.Abs(distance) * (-1);
                break;

            case "D":
                result.Y = Math.Abs(distance) * (-1);
                break;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public IEnumerable <string> GetLogFromMultipleInstructionsAsString(CoOrdinates centralPort, string stringOfInstructions)
        {
            char[] delimiterChars = { ',' };
            var    instructions   = stringOfInstructions.Split(delimiterChars);


            var start = new CoOrdinates()
            {
                X = centralPort.X, Y = centralPort.Y
            };
            var log = new List <string>();

            foreach (var instruction in instructions)
            {
                IEnumerable <CoOrdinates> result = GetLogFromInstruction(start, instruction).ToList();
                log.AddRange(result.Select(x => $"{x.X},{x.Y}"));
                start.X = result.Last().X;
                start.Y = result.Last().Y;
            }

            return(log);
        }
Ejemplo n.º 3
0
        public IEnumerable <CoOrdinates> GetLogFromInstruction(CoOrdinates startingPoint, string instruction)
        {
            var convertedInstruction = TransformInstructionToDistance(instruction);
            var coOrdinatesLog       = new List <CoOrdinates>();
            var currentPosition      = startingPoint;

            var stepsX = convertedInstruction.X;
            var stepsY = convertedInstruction.Y;

            for (int i = 0; i < (Math.Abs(stepsX + stepsY)); i++)
            {
                var temporaryLog = new CoOrdinates()
                {
                    X = currentPosition.X, Y = currentPosition.Y
                };
                if (stepsX > 0)
                {
                    temporaryLog.X = currentPosition.X + 1;
                }
                else if (stepsX < 0)
                {
                    temporaryLog.X = currentPosition.X - 1;
                }
                else if (stepsY > 0)
                {
                    temporaryLog.Y = currentPosition.Y + 1;
                }
                else if (stepsY < 0)
                {
                    temporaryLog.Y = currentPosition.Y - 1;
                }

                coOrdinatesLog.Add(temporaryLog);
                currentPosition = coOrdinatesLog.Last();
            }
            return(coOrdinatesLog);
        }
Ejemplo n.º 4
0
        public Dictionary <string, int> GetLogFromMultipleInstructionsAsDictionary(CoOrdinates centralPort, string instructions)
        {
            var stringResult = GetLogFromMultipleInstructionsAsString(centralPort, instructions);

            return(stringResult.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()));
        }