Example #1
0
        public string RunPart1(IEnumerable <string> testData)
        {
            var commands = testData.ToList();
            var program  = new DuetProgram();

            while (!program.Terminated)
            {
                program.RunNextCommand(commands);
            }

            return(program.CommandsCount["mul"].ToString());
        }
Example #2
0
        public string RunPart2(IEnumerable <string> testData)
        {
            var commands = testData.ToList();
            var a        = new DuetProgram(new Dictionary <char, long> {
                { 'p', 0 }
            });
            var b = new DuetProgram(new Dictionary <char, long> {
                { 'p', 1 }
            });

            while (!a.Terminated && !b.Terminated)
            {
                a.RunNextCommand(commands, b);
                b.RunNextCommand(commands, a);
            }

            return(b.SentCount.ToString());
        }
Example #3
0
        public override int SolvePart2()
        {
            var program0 = new DuetProgram(instructions, 0);
            var program1 = new DuetProgram(instructions, 1);

            program0.LinkedProgram = program1;
            program1.LinkedProgram = program0;

            int valuesSent = 0;

            program1.ValueSent += _ =>
            {
                valuesSent++;
                return(false);
            };

            do
            {
                program0.ResumeExecution();
                program1.ResumeExecution();
            }while (!program0.Deadlocked);
            return(valuesSent);
        }