Example #1
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));
        }
Example #2
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));
        }
        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);
        }