static void BruteInfiniteLoop(string[] instructions) { var nopToJmp = BruteInfiniteLoopNopToJmp(instructions); if (nopToJmp is not null) { Console.WriteLine(StupidGameEmulator.CalculateAccUntilLoop(nopToJmp)); return; } var jmpToNop = BruteInfiniteLoopJmpToNop(instructions); Console.WriteLine(StupidGameEmulator.CalculateAccUntilLoop(jmpToNop)); }
static async Task Main(string[] args) { //var instructions = new[] { // "nop +0", // "acc +1", // "jmp +4", // "acc +3", // "jmp -3", // "acc -99", // "acc +1", // "jmp -4", // "acc +6", //}; var instructions = await File.ReadAllLinesAsync("input.txt"); Console.WriteLine("Value of accumulator before loop: {0}", StupidGameEmulator.CalculateAccUntilLoop(instructions)); BruteInfiniteLoop(instructions); }
static string[] BruteInfiniteLoopJmpToNop(string[] instructions) { bool found = false; int pointer = 0; string[] copyInstructions = null; while (!found && (pointer < instructions.Length)) { copyInstructions = instructions.ToArray(); if (copyInstructions[pointer].StartsWith("jmp")) { copyInstructions[pointer] = copyInstructions[pointer].Replace("jmp", "nop"); found = !StupidGameEmulator.ProgramHasInfiniteLoop(copyInstructions); } pointer++; } return(found ? copyInstructions : null); }