示例#1
0
        public override string SolvePartOne()
        {
            GameProgram gameProgram = new GameProgram(_instructions);

            gameProgram.Run();
            return(gameProgram.Accumulator.ToString());
        }
示例#2
0
        public override string SolvePartTwo()
        {
            int        swapIndex   = 0;
            List <int> swapIndices = new List <int>();

            while (swapIndex < _instructions.Count)
            {
                swapIndex = _instructions.FindIndex(swapIndex, pInstruction =>
                                                    pInstruction.Op == "nop" && pInstruction.Arg != 0 ||
                                                    pInstruction.Op == "jmp");

                if (swapIndex == -1)
                {
                    break;
                }

                swapIndices.Add(swapIndex);
                swapIndex++;
            }

            // PrintInstructions(_instructions);

            foreach (int index in swapIndices)
            {
                List <Instruction> instructions = CopyInstructions();
                instructions[index] = instructions[index].Flip();
                // PrintInstructions(instructions);
                GameProgram gameProgram = new GameProgram(instructions);
                gameProgram.Run();
                if (!gameProgram.DidTerminate())
                {
                    continue;
                }

                return(gameProgram.Accumulator.ToString());
            }

            return("FAILED");
        }