Esempio n. 1
0
        public State Run(bool debug = false)
        {
            Debug = debug;
            if (debug)
            {
                _memory.SetDebugMode();
            }
            while (_entity.Instruction != Instruction.Terminate)
            {
                _entity = GetInstruction(_index);
                if (debug)
                {
                    WriteLine($"Executing {_entity.Instruction} [{_index}]");
                }

                if (!_entity.Execute())
                {
                    return(State.WaitingForInput);
                }

                if (_jumpTo >= 0)
                {
                    _index  = _jumpTo;
                    _jumpTo = -1;
                }
                else
                {
                    _index += _entity.Step;
                }
            }

            return(State.Finished);
        }
Esempio n. 2
0
 public Computer(List <long> instructions, long firstInput)
 {
     _inputs       = new List <long>(new[] { firstInput });
     _instructions = new List <long>(instructions);
     _jumpTo       = -1;
     _entity       = InstructionEntity.NoInstruction;
     _index        = 0;
 }
Esempio n. 3
0
        public long Run(long noun, long verb)
        {
            var instructions = new List <long>(_instructions);

            instructions[1] = noun;
            instructions[2] = verb;

            var entity = InstructionEntity.NoInstruction;
            int index  = 0;

            while (entity.Instruction != Instruction.Terminate)
            {
                entity = InstructionEntity.Get(instructions, index);
                entity.Execute();
                index += entity.Size;
            }

            return(instructions[0]);
        }
Esempio n. 4
0
 public Computer(Memory memory, long?firstInput = null, long?steadyInput = null)
 {
     if (firstInput != null && steadyInput != null)
     {
         throw new InvalidOperationException("Cannot provide both steady input and first input to computer.");
     }
     _inputs = new List <long>();
     if (firstInput != null)
     {
         _inputs.Add(firstInput.Value);
     }
     if (steadyInput != null)
     {
         _steadyInput = steadyInput;
     }
     _memory = memory;
     _jumpTo = -1;
     _entity = InstructionEntity.NoInstruction;
     _index  = 0;
 }