Пример #1
0
        public static long ExecuteAmplifierSequence(Intcode.Interpreter intcode, IEnumerable <long> phases)
        {
            long inputOutput = 0;

            foreach (var phase in phases)
            {
                intcode.SetInput(phase);
                inputOutput = intcode.ExecuteProgram_InputOutput(inputOutput);
            }
            return(inputOutput);
        }
        public void ExecuteInstruction_InputThenOutput()
        {
            // Arrange
            var program = "3,0,4,0,99";
            var intcode = new Intcode.Interpreter(program);
            var input   = 13;

            // Act & Assert
            intcode.SetInput(input);
            intcode.ExecuteInstruction();
            Assert.AreEqual("13,0,4,0,99", intcode.GenerateProgramString());

            intcode.ExecuteInstruction();
            Assert.AreEqual(input, intcode.GetOutput());
        }
Пример #3
0
        // == == == == == Puzzle 2 == == == == ==
        public static string Puzzle2(string input)
        {
            var  outCh         = Channel.CreateUnbounded <long>();
            var  ampA          = new Intcode.Interpreter(input, inputChannel: outCh);
            var  ampB          = new Intcode.Interpreter(input, inputChannel: ampA.OutputChannel);
            var  ampC          = new Intcode.Interpreter(input, inputChannel: ampB.OutputChannel);
            var  ampD          = new Intcode.Interpreter(input, inputChannel: ampC.OutputChannel);
            var  ampE          = new Intcode.Interpreter(input, inputChannel: ampD.OutputChannel, outputChannel: outCh);
            long highestSignal = 0;

            foreach (var phaseSettings in phasePermutationsHigh)
            {
                // Reset and start all amps with new phase input
                var tasks = new Task[5]
                {
                    ampA.ExecuteProgram_StartAsync(phaseSettings.ElementAt(0)),
                    ampB.ExecuteProgram_StartAsync(phaseSettings.ElementAt(1)),
                    ampC.ExecuteProgram_StartAsync(phaseSettings.ElementAt(2)),
                    ampD.ExecuteProgram_StartAsync(phaseSettings.ElementAt(3)),
                    ampE.ExecuteProgram_StartAsync(phaseSettings.ElementAt(4)),
                };

                // Start feedbackloop
                ampA.SetInput(0);

                //Wait for all tasks to reach halt
                Task.WaitAll(tasks);

                // Check if result is largest
                var result = ampE.GetOutput();
                if (result > highestSignal)
                {
                    highestSignal = result;
                }
            }

            return(highestSignal.ToString());
        }