Esempio n. 1
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());
        }