public IList <IInstruction> Parse(string input) { List <IInstruction> instructions = new List <IInstruction>(); //e.g. LMLMLMLMM foreach (char c in input.ToUpper()) { IInstruction newInstruction = new NullInstruction();; switch (c) { case 'L': newInstruction = new LeftInstruction(); break; case 'M': newInstruction = new MoveInstruction(); break; case 'R': newInstruction = new RightInstruction(); break; default: break; } instructions.Add(newInstruction); } return(instructions); }
private static int[] Compute(int[] input) { int i = 0; int opcode = -1; while (i < input.Length && opcode != 99) { Instruction instruction = new NullInstruction(input, i); opcode = input[i] % 100; var fullOpCode = input[i]; switch (opcode) { case 99: break; case 1: instruction = new Add(input, i); break; case 2: instruction = new Multiply(input, i); break; case 3: instruction = new Input(input, i); break; case 4: instruction = new Output(input, i); break; case 5: instruction = new JumpIfTrue(input, i); break; case 6: instruction = new JumpIfFalse(input, i); break; case 7: instruction = new LessThan(input, i); break; case 8: instruction = new Equal(input, i); break; default: throw new ArgumentException(); } (var newInput, var instructionPointer) = instruction.Compute(); i = instructionPointer; input = newInput; } return(input); }