Example #1
0
        /// <summary>
        /// Resets the int code computer to its starting status
        /// </summary>
        public void Reset()
        {
            State = IntCodeComputerState.Idle;
            Code  = _originalCode.ToList();

            _currentInstruction = null;

            _operations = 0;
        }
Example #2
0
        /// <summary>
        /// Exxecutes the defined function for a given opCode
        /// </summary>
        /// <returns></returns>
        private void ExecuteOpCode()
        {
            UpdateInstructionPointer();

            switch (_currentInstruction.OpCode)
            {
            case InstructionModes.Addition:
                Addition();
                break;

            case InstructionModes.Multiplication:
                Multiplication();
                break;

            case InstructionModes.Input:
                Input().Wait();
                break;

            case InstructionModes.Output:
                Output();
                if (!ContinueAfterOutput)
                {
                    State = IntCodeComputerState.ProducedOutput;
                }
                break;

            case InstructionModes.JumpIfTrue:
                JumpIfTrue();
                break;

            case InstructionModes.JumpIfFalse:
                JumpIfFalse();
                break;

            case InstructionModes.LessThan:
                LessThan();
                break;

            case InstructionModes.Equals:
                Equals();
                break;

            case InstructionModes.RelativeBaseOffset:
                RelativeBaseOffset();
                break;

            case InstructionModes.Exit:
                State = IntCodeComputerState.Finished;
                break;

            default:
                throw new ArgumentException($"OpCode {_currentInstruction.OpCode} unkonwn");
            }

            _operations++;
        }
Example #3
0
        /// <summary>
        /// Reads an input parameter
        /// </summary>
        private async Task Input()
        {
            State = IntCodeComputerState.WaitingForInput;

            var input = await InputReader.RequestInput().ConfigureAwait(false);

            State = IntCodeComputerState.Running;

            _currentInstruction.SetValue(ref Code, 0, input);
        }
Example #4
0
 /// <summary>
 /// Gets the opCode from the specified index and calls the specified
 /// method for that code. With the new code this function is called again,
 /// until the opCode 99 is hit
 /// </summary>
 /// <returns></returns>
 public void Compute()
 {
     try
     {
         State = IntCodeComputerState.Running;
         while (State == IntCodeComputerState.Running)
         {
             ExecuteOpCode();
         }
     }
     catch (Exception e)
     {
         State = IntCodeComputerState.Failed;
         throw e;
     }
 }