コード例 #1
0
        public static int SolvePart2(int[] code)
        {
            var phaseSettings = new int[] { 5, 6, 7, 8, 9 };

            int maxOutput = 0;

            do
            {
                var cpus       = new IntCodeCPU[5];
                int prevOutput = 0;
                while (!(cpus[4]?.Halted ?? false))
                {
                    for (int i = 0; i < 5; ++i)
                    {
                        cpus[i] ??= new ((int[])code.Clone(), new int[] { phaseSettings[i] });

                        cpus[i].Inputs.Enqueue(prevOutput);
                        cpus[i].RunUntilWaitingInput();

                        prevOutput = cpus[i].Outputs.Last();
                    }
                }

                if (maxOutput < prevOutput)
                {
                    maxOutput = prevOutput;
                }
            } while(NextPermutation(phaseSettings));

            return(maxOutput);
        }
コード例 #2
0
        public static int SolvePart1(int[] code)
        {
            var phaseSettings = new int[] { 0, 1, 2, 3, 4 };
            int maxOutput     = 0;

            do
            {
                int prevOutput = 0;
                for (int i = 0; i < 5; ++i)
                {
                    int[] localCode = (int[])code.Clone();
                    var   cpu       = new IntCodeCPU(localCode, new int[] { phaseSettings[i], prevOutput });
                    cpu.Run();

                    prevOutput = cpu.Outputs.Last();
                }

                if (maxOutput < prevOutput)
                {
                    maxOutput = prevOutput;
                }
            } while(NextPermutation(phaseSettings));

            return(maxOutput);
        }