Ejemplo n.º 1
0
    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);
    }