Ejemplo n.º 1
0
        private void GetInput(Instruction instruction)
        {
            Console.WriteLine("{0:d6} : GetInput", _instructionPointer);
            // Get input and save at location specified by arg1
            var saveLocation = Memory.GetValue(_instructionPointer + 1, ParameterMode.Immediate);
            var input        = _inputSender.Dequeue();

            Memory.SetValueImmediate(saveLocation, input);
            Goto(_instructionPointer + 2);
        }
Ejemplo n.º 2
0
        public void X()
        {
            //var code = File.ReadLines(".\\Puzzle5.txt").First();
            var code         = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0";
            var interpreter  = new InterpreterBuilder().Build();
            var instructions = interpreter.Interpret(code);

            int result =
                Enumerable.Range(0, 5)
                .GetPermutations()
                .Select(phaseSettingPermutation =>
            {
                var test = new List <Amplifier>();
                IInputSender connector = null;
                foreach (var phaseSetting in phaseSettingPermutation)
                {
                    var inputSender    = connector ?? new ReceiverSenderConnector();
                    var outputReceiver = new ReceiverSenderConnector();

                    inputSender.Enqueue(phaseSetting);

                    if (connector == null)
                    {
                        inputSender.Enqueue(0);
                    }

                    var parser   = new InstructionParser();
                    var computer = new IntcodeComputer(inputSender, outputReceiver, parser);
                    test.Add(new Amplifier(computer, instructions));

                    connector = outputReceiver;
                }

                foreach (var amplifier in test)
                {
                    amplifier.Run();
                }

                return(connector.Dequeue());
            })
                .Largest();

            Assert.AreEqual(43210, result);
        }