示例#1
0
        // Run program with input 1.
        public string PartOne(string[] lines)
        {
            var c = new CPU(lines[0], 10000);

            c.Input.Enqueue(1);
            c.RunWhilePossible();
            return(c.Output.Dequeue().ToString());
        }
示例#2
0
        // Win the game and report the resulting score.
        public string PartTwo(string[] lines)
        {
            var cpu = new CPU(lines[0], 10000);

            cpu.OverwriteMemoryAt(0, 2);

            return(RunGameLoop(cpu).ToString());
        }
示例#3
0
        // Wins the game and returns the score.
        private int RunGameLoop(CPU cpu)
        {
            var paddleState = (x : 0, y : 0);
            var ballState   = (x : 0, y : 0);
            var score       = 0;

            while (cpu.State != CPU.ExecutionState.Halted)
            {
                cpu.RunWhilePossible();
                UpdateGameState(cpu, ref score, ref paddleState, ref ballState);
                cpu.Input.Enqueue(Math.Sign(ballState.x - paddleState.x));
            }

            return(score);
        }
示例#4
0
        // Run the program and count how many breakable blocks (code 2) it printed.
        public string PartOne(string[] lines)
        {
            var cpu = new CPU(lines[0], 10000);

            cpu.RunWhilePossible();

            int result = 0;

            while (cpu.Output.Count > 0)
            {
                cpu.Output.Dequeue();
                cpu.Output.Dequeue();
                if (cpu.Output.Dequeue() == 2)
                {
                    result++;
                }
            }

            return(result.ToString());
        }
示例#5
0
 // Reads all the output of the CPU and updates the relevant game state as necessary.
 private void UpdateGameState(CPU cpu, ref int score, ref (int x, int y) paddle, ref (int x, int y) ball)