Exemple #1
0
            public char[] Process(char[] input)
            {
                var indexX = Array.IndexOf(input, X);
                var indexY = Array.IndexOf(input, Y);

                return(PositionSwap.SwapPositions(input, indexX, indexY));
            }
Exemple #2
0
        private static List <IInstruction> GetInstructions(IEnumerable <string> instructionLines)
        {
            var          instructions = new List <IInstruction>();
            IInstruction instruction;

            foreach (var instructionLine in instructionLines)
            {
                instruction = null;

                var parts = GetParts(instructionLine);

                if (parts[0] == "swap")
                {
                    if (parts[1] == "position")
                    {
                        instruction = new PositionSwap()
                        {
                            IndexX = int.Parse(parts[2]),
                            IndexY = int.Parse(parts[5])
                        };
                    }
                    else
                    {
                        instruction = new LetterSwap()
                        {
                            X = parts[2][0],
                            Y = parts[5][0]
                        };
                    }
                }
                else if (parts[0] == "rotate")
                {
                    if (parts[1] == "based")
                    {
                        instruction = new RotatePos()
                        {
                            X = parts[6][0]
                        };
                    }
                    else
                    {
                        instruction = new Rotate()
                        {
                            Count = int.Parse(parts[2]) * (parts[1] == "right" ? -1 : 1)
                        };
                    }
                }
                else if (parts[0] == "reverse")
                {
                    instruction = new Reverse()
                    {
                        IndexX = int.Parse(parts[2]),
                        IndexY = int.Parse(parts[4])
                    };
                }
                else if (parts[0] == "move")
                {
                    instruction = new Move()
                    {
                        IndexX = int.Parse(parts[2]),
                        IndexY = int.Parse(parts[5])
                    };
                }
                if (instruction == null)
                {
                    throw new InvalidOperationException();
                }

                instructions.Add(instruction);
            }
            return(instructions);
        }