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 };
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);
public void PartOne() => Output .WriteLine($"Accumulator without repetition: {ConsoleVM.ImportProgram(Input).RunProgram().Accumulator}");