Ejemplo n.º 1
0
        public bool IsPrime(int candidate, int degree)
        {
            int mx = _primes.Max();

            if (mx >= Convert.ToInt32(Math.Sqrt(candidate)))
            {
                var t = Execute(2, candidate / 2, degree);
            }
            return(CheckPrime(candidate));
        }
Ejemplo n.º 2
0
        public List <int> Execute(int minPrime, int maxPrime, int degree)
        {
            var subTasks = new Task[degree];

            int mx = _primes.Max();

            if (mx < maxPrime)
            {
                int current = mx + 1;

                for (int i = 0; i < degree; i++, current++)
                {
                    int c1 = current;
                    subTasks[i] = new Task(() => { if (CheckPrime(c1))
                                                   {
                                                       AddToPrimesList(c1);
                                                   }
                                           });
                    subTasks[i].Start();
                }

                while (current < maxPrime)
                {
                    int t = Task.WaitAny(subTasks);
                    int c = current;

                    subTasks[t].Dispose();
                    subTasks[t] = new Task(() => { if (CheckPrime(c))
                                                   {
                                                       AddToPrimesList(c);
                                                   }
                                           });
                    subTasks[t].Start();

                    current++;
                }
                Task.WaitAll(subTasks);
            }
            return(_primes.ToList());
        }
Ejemplo n.º 3
0
        public object Solve()
        {
            var program = GetProgram();
            var input   = new BlockingCollection <long>();
            var output  = new BlockingCollection <long>();

            var computer = new IntcodeComputer();

            var runTask = Task.Run(() =>
            {
                while (!computer.IsHalted)
                {
                    computer.Run(program, input, output);
                }
            });

            long answer = 0;

            var summingTask = Task.Run(() =>
            {
                var currentLocation = new Point(0, 0);
                var view            = new Dictionary <Point, char>();

                while (!computer.IsHalted || output.Count > 0)
                {
                    if (!output.TryTake(out var result, TimeSpan.FromMilliseconds(25)))
                    {
                        break;
                    }

                    if (result == Ascii.NewLine)
                    {
                        Console.WriteLine();

                        currentLocation = new Point(0, currentLocation.Y + 1);
                    }
                    else
                    {
                        var character = (char)result;
                        Console.Write(character);

                        view[currentLocation] = character;

                        currentLocation = new Point(currentLocation.X + 1, currentLocation.Y);
                    }
                }

                var movementRules        = GetMovementRules(view);
                var movementInstructions = GenerateMovementInstructions(movementRules);

                // As instructed
                program = GetProgram(2);

                foreach (var movementInstruction in movementInstructions)
                {
                    input.Add(movementInstruction);
                }

                computer.Run(program, input, output);

                answer = output.Max();
            });

            Task.WaitAll(runTask, summingTask);

            return(answer);
        }