public string Process(string program, int startIndex = 0)
        {
            var output = program;

            while (true)
            {
                var intList = _validator.SplitString(output).ToArray();
                var opCode  = ProgramConfiguration.GetOpCode(intList[startIndex].ToString());
                if (opCode == 99)
                {
                    return(output);
                }
                var logic = _intcodeProgramFactory.GetProgram(opCode);

                if (opCode == 4)
                {
                    _diagnosticCode = logic.Process(output, startIndex);
                }
                else if (opCode == 5 || opCode == 6)
                {
                    logic.Process(output, startIndex);
                }
                else
                {
                    output = (opCode == 3)
                        ? logic.Process(output, _systemIdentifier, startIndex)
                        : logic.Process(output, startIndex);
                }

                startIndex += logic.InstructionLength;
            }
        }
예제 #2
0
        public string Process(string program, int input, int startIndex)
        {
            if (!_intCodeValidator.Validate(program))
            {
                throw new Exception("Invalid program");
            }
            _intCodeValidator.ValidateProgramConfiguration(program, startIndex, 3);
            var intList = _intCodeValidator.SplitString(program).ToArray();

            var target = intList[startIndex + 1];

            intList[target] = input;
            return(_intCodeValidator.Join(intList));
        }
예제 #3
0
        public string Process(string program, int startIndex = 0)
        {
            if (!_intCodeValidator.Validate(program))
            {
                throw new Exception("Program input is invalid");
            }
            _intCodeValidator.ValidateProgramConfiguration(program, startIndex, 4);
            var intList = _intCodeValidator.SplitString(program).ToArray();

            ProgramConfiguration.GetParameterMode(program, out var position1, out var ignored, out var unused, startIndex);
            _intCodeValidator.ExtractOrdinals(intList, out var i1, out var i2, out var pointer, startIndex);
            var value = (position1 == ParameterMode.Immediate) ? intList[startIndex + 1] : intList[i1];

            return(value.ToString());
        }
        public string Process(string program, int startIndex = 0)
        {
            if (!_intCodeValidator.Validate(program))
            {
                throw new Exception("Program input is invalid");
            }
            _intCodeValidator.ValidateProgramConfiguration(program, startIndex, 7);
            var intList = _intCodeValidator.SplitString(program).ToArray();

            _intCodeValidator.ExtractOrdinals(intList, out var i1, out var i2, out var pointer, startIndex: startIndex);
            ProgramConfiguration.GetParameterMode(program, out var arg1Mode, out var arg2Mode, out var pointerMode, startIndex);
            var arg1 = (arg1Mode == ParameterMode.Immediate) ? intList[startIndex + 1] : intList[i1];
            var arg2 = (arg2Mode == ParameterMode.Immediate) ? intList[startIndex + 2] : intList[i2];

            intList[pointer] = (arg1 < arg2) ? 1 : 0;
            return(_intCodeValidator.Join(intList));
        }
예제 #5
0
        public string Process(string program, int startIndex = 0)
        {
            if (!_validator.Validate(program))
            {
                throw new Exception("Invalid input: must be comma separated list of numbers");
            }
            _validator.ValidateProgramConfiguration(program, startIndex, 1);

            var intList = _validator.SplitString(program).ToArray();

            _validator.ExtractOrdinals(intList, out var i1, out var i2, out var pointer, startIndex: startIndex);
            ProgramConfiguration.GetParameterMode(program, out var arg1Mode, out var arg2Mode, out var writeParameterMode, startIndex: startIndex);
            var arg1 = (arg1Mode == ParameterMode.Immediate) ? intList[startIndex + 1] : intList[i1];
            var arg2 = (arg2Mode == ParameterMode.Immediate) ? intList[startIndex + 2] : intList[i2];

            intList[pointer] = arg1 + arg2;
            return(_validator.Join(intList));
        }
        public string Process(string program, int startIndex = 0)
        {
            _jump = 3;

            if (!_intCodeValidator.Validate(program))
            {
                throw new Exception("Program input is invalid");
            }
            _intCodeValidator.ValidateProgramConfiguration(program, startIndex, 6);
            var intList = _intCodeValidator.SplitString(program).ToArray();

            _intCodeValidator.ExtractOrdinals(intList, out var i1, out var pointer, out var ignored, startIndex: startIndex);
            ProgramConfiguration.GetParameterMode(program, out var arg1Mode, out var arg2Mode, out var pointerMode, startIndex);
            var arg1 = (arg1Mode == ParameterMode.Immediate) ? intList[startIndex + 1] : intList[i1];
            var arg2 = (arg2Mode == ParameterMode.Immediate) ? intList[startIndex + 2] : intList[pointer];

            if (arg1 == 0)
            {
                _jump = arg2 - startIndex;
            }

            return(string.Empty);
        }