protected override int SolvePartOne() { AssemblyProgram program = new AssemblyProgram(lines); InfiniteLoopDetector loopDetector = new InfiniteLoopDetector(program); loopDetector.Execute(); return(program.Accumulator); }
protected override int SolvePartTwo() { Queue <int> indicesWhichContainJmp = new Queue <int>(); Queue <int> indicesWhichContainNop = new Queue <int>(); // find all lines which contain a nop or jmp instruction for (int i = 0; i < lines.Length; i++) { if (lines[i].StartsWith("nop")) { indicesWhichContainNop.Enqueue(i); } else if (lines[i].StartsWith("jmp")) { indicesWhichContainJmp.Enqueue(i); } } InfiniteLoopDetector loopDetector; AssemblyProgram program; // iterate all lines which contain nop instruction do { int index = indicesWhichContainNop.Dequeue(); lines[index] = lines[index].Replace("nop", "jmp"); program = new AssemblyProgram(lines); loopDetector = new InfiniteLoopDetector(program); // restore original program lines[index] = lines[index].Replace("jmp", "nop"); } while (loopDetector.Execute() && indicesWhichContainNop.Count > 0); int answer; if (indicesWhichContainNop.Count > 0) { answer = program.Accumulator; } else { do { int index = indicesWhichContainJmp.Dequeue(); lines[index] = lines[index].Replace("jmp", "nop"); program = new AssemblyProgram(lines); loopDetector = new InfiniteLoopDetector(program); // restore original program lines[index] = lines[index].Replace("nop", "jmp"); } while (loopDetector.Execute() && indicesWhichContainJmp.Count > 0); answer = program.Accumulator; } return(answer); }
public override string Part1(string input, object?args) { var rq = new List <long>(); var sq = new List <long>(); var p0 = new AssemblyProgram(0, ref rq, ref sq, input); p0.RunProgram(true); return(p0.LastFrequencySent.ToString()); }
public override string Part1(string input) { List <long> reQ = null; List <long> seQ = null; var m = new AssemblyProgram(1, ref reQ, ref seQ, input); m.RunProgram(false); return(m.MulInvoked.ToString()); }
public override string Part2(string input) { List <long> reQ = null; List <long> seQ = null; var m = new AssemblyProgram(1, ref reQ, ref seQ, input); m.Registers.Add("a", 1); m.RunProgram(false); return(m.Registers["h"].ToString()); ; }
public override string Part2(string input, object?args) { var q0 = new List <long>(); var q1 = new List <long>(); var p0 = new AssemblyProgram(0, ref q0, ref q1, input); var p1 = new AssemblyProgram(1, ref q1, ref q0, input); do { p0.RunProgram(false); p1.RunProgram(false); } while (p0.SentMsgToQ || p1.SentMsgToQ); return(p1.MessagesSent.ToString()); }
public abstract void Execute(AssemblyProgram program);
public override void Execute(AssemblyProgram program) { program.InstructionPointer += 1; }
public override void Execute(AssemblyProgram program) { program.Accumulator += Argument; program.InstructionPointer += 1; }