Beispiel #1
0
    public static long Run(List <long> Program)
    {
        CurrentProgram = Program;
        IndexPointer   = 0;
        while (true)
        {
            Console.WriteLine("IndexPointer: " + IndexPointer);
            char[] opcode = ProcessOpcode(Program[(int)IndexPointer]);
            if (new string(opcode, 3, 2) == "99")
            {
                Console.WriteLine("In the 99");
                long output = Output.Last();
                IndexPointer = -1;
                SaveMemory();
                return(output);
            }

            long FirstParameter;
            long SecondParameter;
            switch (new string(opcode, 3, 2))
            {
            case "01":
                FirstParameter  = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                SecondParameter = GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                SetByIndex(IndexPointer + 3, FirstParameter + SecondParameter, GetMode(opcode, 0));
                IndexPointer += 4;
                break;

            case "02":
                FirstParameter  = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                SecondParameter = GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                SetByIndex(IndexPointer + 3, FirstParameter * SecondParameter, GetMode(opcode, 0));
                IndexPointer += 4;
                break;

            case "03":
                SetByIndex(IndexPointer + 1, SystemId.First(), GetMode(opcode, 2));
                SystemId.Dequeue();
                IndexPointer += 2;
                break;

            case "04":
                Output.Add(GetByIndex(IndexPointer + 1, GetMode(opcode, 2)));
                IndexPointer += 2;
                SaveMemory();
                return(Output.Last());

            case "05":
                FirstParameter = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                if (!FirstParameter.Equals(0))
                {
                    IndexPointer = (int)GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                }
                else
                {
                    IndexPointer += 3;
                }

                break;

            case "06":
                FirstParameter = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                if (FirstParameter.Equals(0))
                {
                    IndexPointer = (int)GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                }
                else
                {
                    IndexPointer += 3;
                }

                break;

            case "07":
                FirstParameter  = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                SecondParameter = GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                if (FirstParameter < SecondParameter)
                {
                    SetByIndex(IndexPointer + 3, 1, GetMode(opcode, 0));
                }
                else
                {
                    SetByIndex(IndexPointer + 3, 0, GetMode(opcode, 0));
                }

                IndexPointer += 4;
                break;

            case "08":
                FirstParameter  = GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                SecondParameter = GetByIndex(IndexPointer + 2, GetMode(opcode, 1));
                if (FirstParameter.Equals(SecondParameter))
                {
                    SetByIndex(IndexPointer + 3, 1, GetMode(opcode, 0));
                }
                else
                {
                    SetByIndex(IndexPointer + 3, 0, GetMode(opcode, 0));
                }

                IndexPointer += 4;
                break;

            case "09":
                RelativeBase += (int)GetByIndex(IndexPointer + 1, GetMode(opcode, 2));
                IndexPointer += 2;
                break;

            default:
                Console.WriteLine("opcode is out of range!");
                throw new ArgumentOutOfRangeException(nameof(opcode), opcode, "Opcode is OutOfRange");
            }
        }
    }