public void Execute(AdventProgram program) { IP = 0; AX = 0; foreach (var cmd in program.Commands) { Execute(cmd); } }
static void Main(string[] args) { string inputPath = args[0]; StreamReader inputFile = new StreamReader(inputPath); List <string> input = new List <string>(); string line; while ((line = inputFile.ReadLine()) != null) { input.Add(line); } AdventProgram adv = new AdventProgram(); adv.LoadProgram(input); HashSet <int> SeenInstructions = new HashSet <int>(); while (true) { int acc = adv.Accumulator; bool notYetSeen = SeenInstructions.Add(adv.ExecuteOne().ID); if (!notYetSeen) { Console.WriteLine($"Pt1: {acc}"); break; } } // pt 2 // create permutations of input data List <List <string> > possibleInstructionChanges = new List <List <string> >(); for (int i = 0; i < input.Count; i++) { List <string> tmp = new List <string>(input); switch (input[i].Substring(0, 3)) { case "nop": possibleInstructionChanges.Add(tmp); possibleInstructionChanges[^ 1][i] = tmp[i].Replace("nop", "jmp");
static void Main(string[] args) { var program = new AdventProgram(); program.Compile(File.ReadLines("input.txt")); var vm = new AdventVM(); var executed = new HashSet <int>(); while (!executed.Contains(vm.IP)) { executed.Add(vm.IP); vm.Execute(program.Commands[vm.IP]); } Console.WriteLine("Answer #1:" + vm.AX); vm.AX = 0; vm.IP = 0; executed.Clear(); var changeIndex = 0; while (true) { executed.Add(vm.IP); vm.Execute(program.Commands[vm.IP]); if (vm.IP == program.Commands.Count) { break; } if (vm.IP > program.Commands.Count || executed.Contains(vm.IP)) { program = new AdventProgram(); program.Compile(File.ReadLines("input.txt")); while (true) { var txt = program.Commands[changeIndex].Text; if (txt.StartsWith("nop") && int.Parse(txt.Split()[1]) != 0) { program.Commands[changeIndex] = program.CompileOne(txt.Replace("nop", "jmp")); changeIndex++; break; } else if (txt.StartsWith("jmp")) { program.Commands[changeIndex] = program.CompileOne(txt.Replace("jmp", "nop")); changeIndex++; break; } else { changeIndex++; } } executed.Clear(); vm.IP = 0; vm.AX = 0; } } Console.WriteLine("Answer #2:" + vm.AX); }