예제 #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));
        }
        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));
        }
예제 #3
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 Tuple <int, int> GetForOutput(string program, int outputValue)
        {
            var intList = _validator.SplitString(program).ToArray();

            for (var i = 0; i < 100; i++)
            {
                for (var j = 0; j < 100; j++)
                {
                    intList[1] = i;
                    intList[2] = j;
                    var output = Process(_validator.Join(intList));
                    var result = _validator.SplitString(output).ToArray()[0];
                    if (result == outputValue)
                    {
                        return(new Tuple <int, int>(i, j));
                    }
                }
            }
            throw new Exception("Program did not resolve for inputs up to 100");
        }