예제 #1
0
        public static int CalculateResultForPartOne(string[] input)
        {
            var program = new MyProgram(input);

            program.Run();
            return(program.Accumulator);
        }
예제 #2
0
        public static int CalculateResultForPartTwo(string[] input)
        {
            var instructionList    = input.ToList();
            var instructionToAlter = instructionList.FindIndex(i => i.StartsWith("nop") || i.StartsWith("jmp"));

            while (instructionToAlter >= 0)
            {
                AlterInstruction(input, instructionToAlter);
                var program = new MyProgram(input);
                var programEndedSuccessfully = program.Run();

                if (programEndedSuccessfully)
                {
                    return(program.Accumulator);
                }

                // change altered instruction back
                AlterInstruction(input, instructionToAlter);

                // alter next nop or jmp instruction
                instructionToAlter = instructionList.FindIndex(
                    ++instructionToAlter,
                    i => i.StartsWith("nop") || i.StartsWith("jmp"));
            }

            return(int.MinValue);
        }