public void Run() { bool[] hasBeenVisited = new bool[Program.Length]; int programPosition = 0; while (programPosition < Program.Length) { hasBeenVisited[programPosition] = true; InstructionParser parser = new InstructionParser(Program[programPosition]); switch (parser.Command) { case "acc": Accumulator += parser.Value; programPosition++; break; case "jmp": programPosition += parser.Value; break; default: programPosition++; break; } if (programPosition >= Program.Length) { break; } else if (hasBeenVisited[programPosition]) { IsInfiniteLoop = true; break; } } }
public static void Part2(string[] data) { Computer computer = new Computer(new string[0]); for (int i = 0; i < data.Length; i++) { string[] alteredProgram = new string[data.Length]; Array.Copy(data, alteredProgram, data.Length); string newInstruction = string.Empty; InstructionParser parser = new InstructionParser(alteredProgram[i]); if (parser.Command == "nop") { newInstruction = "jmp " + parser.Value.ToString("+#;-#;+0"); } else if (parser.Command == "jmp") { newInstruction = "nop " + parser.Value.ToString("+#;-#;+0"); } else { continue; } alteredProgram[i] = newInstruction; computer = new Computer(alteredProgram); computer.Run(); if (!computer.IsInfiniteLoop) { break; } } Console.WriteLine($"The value of the accumulator is: {computer.Accumulator}."); }