Example #1
0
        private int ProcessOpcodeAndMove(long opcode, Mode nounMode, Mode verbMode, Mode resMode, int ptr)
        {
            switch (opcode)
            {
            case 1:
                SetMemory(_memory[ptr + 3], resMode, GetValueByMode(nounMode, _memory[ptr + 1]) + GetValueByMode(verbMode, _memory[ptr + 2]));
                return(ptr + 4);

            case 2:
                SetMemory(_memory[ptr + 3], resMode, GetValueByMode(nounMode, _memory[ptr + 1]) * GetValueByMode(verbMode, _memory[ptr + 2]));
                return(ptr + 4);

            case 3:
                SetMemory(_memory[ptr + 1], nounMode, _input.GetNext());
                return(ptr + 2);

            case 4:
                _output.Send(GetValueByMode(nounMode, _memory[ptr + 1]));
                return(ptr + 2);

            case 5:
                if (GetValueByMode(nounMode, _memory[ptr + 1]) != 0)
                {
                    return((int)GetValueByMode(verbMode, _memory[ptr + 2]));
                }

                return(ptr + 3);

            case 6:
                if (GetValueByMode(nounMode, _memory[ptr + 1]) == 0)
                {
                    return((int)GetValueByMode(verbMode, _memory[ptr + 2]));
                }

                return(ptr + 3);

            case 7:
                SetMemory(_memory[ptr + 3], resMode, GetValueByMode(nounMode, _memory[ptr + 1]) < GetValueByMode(verbMode, _memory[ptr + 2])
                                                        ? 1
                                                        : 0);
                return(ptr + 4);

            case 8:
                SetMemory(_memory[ptr + 3], resMode, GetValueByMode(nounMode, _memory[ptr + 1]) == GetValueByMode(verbMode, _memory[ptr + 2])
                                                        ? 1
                                                        : 0);
                return(ptr + 4);

            case 9:
                _relativeBase += GetValueByMode(nounMode, _memory[ptr + 1]);
                return(ptr + 2);

            default:
                throw new NotImplementedException($"Invalid value {opcode}");
            }
        }