static void Main(string[] args) { var parsedInput = File.ReadAllLines("input.txt"); var instructions = new List <Instruction>(); parsedInput.ToList().ForEach(instruction => instructions.Add(new Instruction(instruction))); var executionUnit = new ExecutionUnit(); Console.WriteLine("Executing until loop is found on next instruction:"); executionUnit.ExecuteProgram(instructions.ToArray(), true); Console.WriteLine("\nExecuting until the program works:"); ExecuteUntilWorkingProgramFound(executionUnit, instructions); }
public static void ExecuteUntilWorkingProgramFound(ExecutionUnit executionUnit, List <Instruction> instructions) { var successfulExecution = false; var manipulatedInstructionIndex = 0; while (!successfulExecution) { var instructionToManipulate = instructions[manipulatedInstructionIndex]; if (instructionToManipulate.Type != InstructionType.Acc) { instructionToManipulate.Type = FlipInstructionType(instructionToManipulate); } else { manipulatedInstructionIndex++; continue; } successfulExecution = executionUnit.ExecuteProgram(instructions.ToArray(), true); instructionToManipulate.Type = FlipInstructionType(instructionToManipulate); manipulatedInstructionIndex++; } }