/// <summary>
        /// Start the computer a single time, and return the last output.
        /// </summary>
        /// <param name="program">
        /// A comma-separated string of integer instructions
        /// </param>
        /// <param name="inputs">Program inputs</param>
        /// <returns>The last "output" value output by the computer</returns>
        public static long Run(string program, params long[] inputs)
        {
            IntcodeComputer computer = new IntcodeComputer(program, inputs);

            computer.Start();
            return(computer.Output);
        }
        /// <summary>
        /// Start the computer a single time, and return the last output.
        /// </summary>
        /// <param name="program">
        /// An integer array instructions
        /// </param>
        /// <param name="inputs">Program inputs</param>
        /// <returns>The last "output" value output by the computer</returns>
        public static long Run(ref long[] program, params long[] inputs)
        {
            IntcodeComputer computer = new IntcodeComputer(program, inputs);

            computer.Start();
            program = computer.Program.ToArray();
            return(computer.Output);
        }
Beispiel #3
0
        public override string A()
        {
            IntcodeComputer brain  = new IntcodeComputer(Input);
            int             blocks = 0;
            int             index  = 1;

            brain.OnOutput += val => {
                if (index++ % 3 == 0 && val == 2)
                {
                    blocks++;
                }
            };
            brain.Start();
            return(blocks.ToString());
        }
Beispiel #4
0
        public override string B()
        {
            long score = 0;

            long[] program = Input.Split(",").Select(long.Parse).ToArray();
            program[0] = 2;
            IntcodeComputer brain = new IntcodeComputer(program);

            long ball   = 0;
            long paddle = 0;
            int  index  = 0;
            long lastX  = 0;

            brain.OnOutput += val => {
                index++;
                if (index == 1)
                {
                    lastX = val;
                }
                else if (index == 3)
                {
                    index = 0;
                    if (lastX == -1)
                    {
                        score = val;
                    }
                    else if (val == 3)
                    {
                        paddle = lastX;
                    }
                    else if (val == 4)
                    {
                        ball = lastX;
                    }
                }
            };

            brain.WantsInput += () => paddle <ball ? 1 : paddle> ball ? -1 : 0;

            brain.Start();
            return(score.ToString());
        }