Ejemplo n.º 1
0
        private static long RunAmplifier(long[] computerInput, out List <long> maxOutputCombination)
        {
            var phaseCombinations = GenerateLists(new List <long>(), new List <long>()
            {
                0, 1, 2, 3, 4
            });
            var maxOutput = long.MinValue;

            maxOutputCombination = new List <long>();
            foreach (var phaseCombination in phaseCombinations)
            {
                long lastOutput = 0;
                foreach (var phase in phaseCombination)
                {
                    var amplifier = new IntComputer((long[])computerInput.Clone());
                    lastOutput = amplifier.Run(new[] { phase, lastOutput });
                }

                if (lastOutput > maxOutput)
                {
                    maxOutput            = lastOutput;
                    maxOutputCombination = phaseCombination;
                }
            }

            return(maxOutput);
        }
Ejemplo n.º 2
0
        private static long RunFeedbackAmplifier(long[] computerInput, out List <long> maxOutputCombination)
        {
            var phaseCombinations = GenerateLists(new List <long>(), new List <long>()
            {
                5, 6, 7, 8, 9
            });
            var maxOutput = long.MinValue;

            maxOutputCombination = new List <long>();
            var combinationCounter = 0;

            foreach (var phaseCombination in phaseCombinations)
            {
                Console.WriteLine("Trying combination {0}", combinationCounter++);
                var lastAmplifier    = new IntComputer(new long[] { 99 });
                var firstInput       = new IntComputerInput();
                var amplifierThreads = new List <Thread>();
                foreach (var phase in phaseCombination)
                {
                    var amplifier = new IntComputer((long[])computerInput.Clone());
                    var input     = new IntComputerInput(phase);
                    if (amplifierThreads.Count == 0)
                    {
                        input.AddValue(0);
                        firstInput = input;
                    }
                    else if (amplifierThreads.Count == 4)
                    {
                        lastAmplifier.Connector = input;
                        amplifier.Connector     = firstInput;
                    }
                    else
                    {
                        lastAmplifier.Connector = input;
                    }
                    var newRunThread = new Thread(() => amplifier.Run(input));
                    amplifierThreads.Add(newRunThread);
                    newRunThread.Start();
                    lastAmplifier = amplifier;
                }


                foreach (var thread in amplifierThreads)
                {
                    thread.Join();
                }

                if (lastAmplifier.GetOutput() > maxOutput)
                {
                    maxOutput            = lastAmplifier.GetOutput();
                    maxOutputCombination = phaseCombination;
                }
            }

            return(maxOutput);
        }