Esempio n. 1
0
        protected override int SolvePartOne()
        {
            AssemblyProgram      program      = new AssemblyProgram(lines);
            InfiniteLoopDetector loopDetector = new InfiniteLoopDetector(program);

            loopDetector.Execute();
            return(program.Accumulator);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
    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());
    }
Esempio n. 4
0
    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());
    }
Esempio n. 5
0
    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());

        ;
    }
Esempio n. 6
0
    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());
    }
Esempio n. 7
0
 public abstract void Execute(AssemblyProgram program);
Esempio n. 8
0
 public override void Execute(AssemblyProgram program)
 {
     program.InstructionPointer += 1;
 }
Esempio n. 9
0
 public override void Execute(AssemblyProgram program)
 {
     program.Accumulator        += Argument;
     program.InstructionPointer += 1;
 }