Ejemplo n.º 1
0
        // == PART II

        /// <summary>A brute-force algorithm to systematically try all possible input pairs
        /// until we find the one that gave the desired output (we can determine a
        /// finished set of possible candidates since we know that each number is in the
        /// [0, 99] range).</summary>
        /// <param name="inputs">List of integers to execute as an Intcode program.</param>
        /// <param name="wantedOutput">Desired output of the program.</param>
        /// <returns>Specific checksum that matches the desired output.</returns>
        static int FindChecksum(int[] inputs, int wantedOutput)
        {
            // prepare program
            IntcodeProgram program = new IntcodeProgram(inputs);

            for (int noun = 0; noun < 100; noun++) // range is [0, 100[ = [0, 99]
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    // reset program to initial state
                    program.Reset();
                    // set up noun and verb
                    program.Program[1] = noun;
                    program.Program[2] = verb;
                    // run and compare result
                    program.Run();
                    if (program.Program[0] == wantedOutput)
                    {
                        return(100 * noun + verb);
                    }
                }
            }

            return(-1);
        }
Ejemplo n.º 2
0
        // [ Computation functions ]
        // -------------------------

        // == PART I

        /// <summary>Executes the Intcode program on the provided inputs and computes the final
        /// result.</summary>
        /// <param name="inputs">List of integers to execute as an Intcode program.</param>
        /// <param name="restoreGravityAssist">Whether or not to restore the gravity assist
        /// by modifying the input program.</param>
        /// <returns>Final output of the program.</returns>
        static int ProcessInputs(int[] inputs, bool restoreGravityAssist = false)
        {
            // restore gravity assist?
            if (restoreGravityAssist)
            {
                inputs[1] = 12;
                inputs[2] = 2;
            }
            // create and execute the program
            IntcodeProgram program = new IntcodeProgram(inputs);

            program.Run();
            // isolate final result
            return(program.Program[0]);
        }