示例#1
0
        public string Solve()
        {
            var lines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim().Split(' ')).ToList();

            var program = new SoundProgram(0, lines);

            while (program.State != State.Terminated)
            {
                program.Step();
            }

            return(program.Answer);
        }
示例#2
0
        protected override string?SolvePartOne()
        {
            var program = new SoundProgram(Input);

            bool ret = false;

            do
            {
                ret = program.Run();
            } while (ret);

            return(program.firstRcv.ToString());
        }
示例#3
0
        static void Main(string[] args)
        {
            var input = File.ReadAllText("input.txt");

            var p0 = SoundProgram.Parse(0, input);
            var p1 = SoundProgram.Parse(1, input);

            Task[] tasks = new Task[2];
            tasks[0] = Task.Factory.StartNew(() => p0.Execute(p1.OutputQueue, p1.WaitingForInput));
            tasks[1] = Task.Factory.StartNew(() => p1.Execute(p0.OutputQueue, p0.WaitingForInput));

            Task.WaitAll(tasks);
            Console.WriteLine($"0: sent: {p0.Registers["SentNumbers"]}, received: {p0.Registers["ReceivedNumbers"]}");
            Console.WriteLine($"1: sent: {p1.Registers["SentNumbers"]}, received: {p1.Registers["ReceivedNumbers"]}");
            Console.WriteLine(p1.SentNumbers);

            Console.Read();
        }
示例#4
0
        public void Test1()
        {
            var input = @"snd 1
snd 2
snd p
rcv a
rcv b
rcv c
rcv d";
            var p0    = SoundProgram.Parse(0, input);
            var p1    = SoundProgram.Parse(1, input);

            Task[] tasks = new Task[2];
            tasks[0] = Task.Factory.StartNew(() => p0.Execute(p1.OutputQueue, p1.WaitingForInput));
            tasks[1] = Task.Factory.StartNew(() => p1.Execute(p0.OutputQueue, p0.WaitingForInput));

            Task.WaitAll(tasks);

            Assert.AreEqual(3, (int)p1.SentNumbers);
        }
示例#5
0
        protected override string?SolvePartTwo()
        {
            var p0 = new SoundProgram(Input, 0);
            var p1 = new SoundProgram(Input, 1);

            // Set the remote queues up
            p0.remote = p1.queue;
            p1.remote = p0.queue;

            bool ret0       = false;
            bool ret1       = false;
            bool deadlocked = false;

            do
            {
                ret0 = p0.Run();
                ret1 = p1.Run();

                // If deadlocked
                deadlocked = p0.IsDeadlocked() && p1.IsDeadlocked();
            } while (ret0 && ret1 && !deadlocked);

            return(p1.sendCount.ToString());
        }