예제 #1
0
        protected override string SolvePartTwo()
        {
            // For each instruction to replace, we will re-run this computer again and again
            foreach (var kvp in instructions)
            {
                // Reinitialize from the beginning
                computer = new GameComputer(Input);

                // Change this one instruction (must be done indirectly since I'm not using a class)
                var current = computer.instructions[kvp.Key];
                current.instruction            = (kvp.Value == GameComputerOps.jmp ? "nop" : "jmp");
                computer.instructions[kvp.Key] = current;

                // Go through each step
                int ret = computer.DoOperation();
                while (ret == 1)
                {
                    ret = computer.DoOperation();
                }

                // Check if we exited normally
                if (ret == 2)
                {
                    break;
                }

                // Otherwise, loop again and try over
            }

            return(computer.accumulator.ToString());
        }
예제 #2
0
        public Day08() : base(08, 2020, "")
        {
            // Load the original computer
            computer = new GameComputer(Input);

            // Let's note all of the positions of jmp and nop
            instructions = new Dictionary <int, GameComputerOps>();
            for (int i = 0; i < computer.instructions.Count; i++)
            {
                var name = computer.ParseInstructionName(computer.instructions[i].instruction);

                // Skip acc instructions for Part 2
                if (name == GameComputerOps.acc)
                {
                    continue;
                }

                // Note the position and instruction we have (to possibly replace)
                instructions.Add(i, name);
            }
        }