Beispiel #1
0
        private static long Part2(IReadOnlyCollection <long> memoryState)
        {
            var amplifiers = new List <long> {
                9, 8, 7, 6, 5
            };
            var amplifiersPermutations = Permutations.GeneratePermutations(amplifiers);

            var signals = amplifiersPermutations.Select(permutation =>
            {
                var amps = permutation
                           .Select(phaseSetting => new IntCodeComputer(memoryState, phaseSetting))
                           .ToList();

                var signal = 0L;

                while (!amps.Last().IsHalted)
                {
                    foreach (var amp in amps)
                    {
                        amp.Inputs.Enqueue(signal);
                        amp.RunIntCode();
                        signal = amp.Output.Last();
                    }
                }

                return(signal);
            });

            return(signals.Max());
        }
Beispiel #2
0
        private static long Part1(IReadOnlyCollection <long> memoryState)
        {
            var amplifiers = new List <long> {
                0, 1, 2, 3, 4
            };
            var amplifiersPermutations = Permutations.GeneratePermutations(amplifiers);

            var signals = amplifiersPermutations.Select(permutation =>
            {
                var signal = 0L;
                foreach (var phaseSetting in permutation)
                {
                    var amp = new IntCodeComputer(memoryState, phaseSetting, signal);
                    amp.RunIntCode();
                    signal = amp.Output.Last();
                }

                return(signal);
            });

            return(signals.Max());
        }