示例#1
0
        public int DebugProgram(string[] input)
        {
            for (var i = 0; i < input.Length; i++)
            {
                var program = ConsoleVM.ParseInput(input);
                if (program[i].Instruction != "nop" && program[i].Instruction != "jmp")
                {
                    continue;
                }

                program[i] = SwapCmd(program[i]);
                var result = ConsoleVM
                             .ImportProgram(program)
                             .RunProgram();

                if (result.Status == ConsoleVM.HaltStatus.InfiniteLoopDetected)
                {
                    continue;
                }

                return(result.Accumulator);
            }

            throw new Exception("Unable to fix program.");

            (string, int) SwapCmd((string, int) instruction) =>
            instruction switch
            {
                ("jmp", int v) => ("nop", v),
                ("nop", int v) => ("jmp", v),
                var i => i
            };
示例#2
0
 public void ExampleOne() =>
 ConsoleVM
 .ImportProgram(new[]
 {
     "nop +0",
     "acc +1",
     "jmp +4",
     "acc +3",
     "jmp -3",
     "acc -99",
     "acc +1",
     "jmp -4",
     "acc +6"
 })
 .RunProgram()
 .Accumulator
 .Should()
 .Be(5);
示例#3
0
 public void PartOne() =>
 Output
 .WriteLine($"Accumulator without repetition: {ConsoleVM.ImportProgram(Input).RunProgram().Accumulator}");