Esempio n. 1
0
    public static long RunSequenceWithFeedbackLoop(long[] intcode, long[] phases)
    {
        var compilers = new IntCodeCompiler[] {
            new IntCodeCompiler(phases[0], true),
            new IntCodeCompiler(phases[1], true),
            new IntCodeCompiler(phases[2], true),
            new IntCodeCompiler(phases[3], true),
            new IntCodeCompiler(phases[4], true)
        };

        var programs = new IntCodeProgram[] {
            new IntCodeProgram(intcode.ToArray(), 0),
            new IntCodeProgram(intcode.ToArray(), 0),
            new IntCodeProgram(intcode.ToArray(), 0),
            new IntCodeProgram(intcode.ToArray(), 0),
            new IntCodeProgram(intcode.ToArray(), 0)
        };


        long input = 0;

        for (int i = 0; i < compilers.Length; i++)
        {
            var compiler = compilers[i];
            var program  = programs[i];
            Debug.Log($"(First Pass)Running Amp {i} at pointer {program.Pointer} with input {input}");
            compiler.Clear();
            compiler.SetInputs(new long[] { phases[i], input });
            programs[i] = compiler.Compute(program);
            input       = compiler.OutputValue;
        }

        int maxLoops = 20;

        while (!programs.Last().IsDone&& maxLoops-- > 0)
        {
            for (int i = 0; i < compilers.Length; i++)
            {
                var compiler = compilers[i];
                var program  = programs[i];
                Debug.Log($"Running Amp {i} at pointer {program.Pointer} with input {input}");
                compiler.Clear();
                compiler.SetInputs(new long[] { input });
                programs[i] = compiler.Compute(program);
                input       = compiler.OutputValue;
            }
        }


        return(input);
    }