public static void MutateToFindNormalExitAccumulator(string filename) { var program = LoadProgram(filename); int[] nopIndexes = FindJmpIndexes(program); int accumulator = 0; for (var i = 0; i < nopIndexes.Length && accumulator == 0; i++) { var nopIndex = nopIndexes[i]; var saved = program[nopIndex]; program[nopIndex] = new SimpleInstruction { Operator = SimpleProgram.Nop, }; try { accumulator = program.Run(); } catch (InfiniteLoopException e) { // Not this one } program[nopIndex] = saved; } Console.WriteLine($"Accumulator value on normal program exit is {accumulator}"); }
private static SimpleProgram LoadProgram(string filename) { var lines = Utility.ReadLinesFromFile(filename); var program = new SimpleProgram(); foreach (var line in lines) { var parts = line.Split(" "); var instruction = new SimpleInstruction { Operator = parts[0], Operand = Convert.ToInt32(parts[1]) }; program.Add(instruction); } return(program); }