Beispiel #1
0
        private static void Part2(DirectedGraph graph)
        {
            var workGroup = new WorkGroup(5);

            var completedSteps = new List <Node>();
            var availableSteps = new List <Node>(graph.Roots);

            availableSteps.Sort();

            // Initialise
            foreach (Worker worker in workGroup.Workers)
            {
                if (availableSteps.Any())
                {
                    worker.AssignStep(availableSteps.First());
                    availableSteps.RemoveAt(0);
                }
            }

            // Tick
            var totalTime = 0;

            while (completedSteps.Count < graph.Nodes.Count)
            {
                var workResult = workGroup.DoWork();
                if (workResult.Any())
                {
                    completedSteps.AddRange(workResult);

                    // Get newly available steps
                    foreach (var stepsReadyToComplete in completedSteps.Select(completedStep =>
                                                                               graph.GetStepsReadyToComplete(completedStep.Name,
                                                                                                             string.Join(string.Empty, completedSteps.Select(c => c.Name)))))
                    {
                        availableSteps.AddRange(stepsReadyToComplete.Select(s => new Node(s)));
                    }

                    availableSteps = availableSteps.DistinctBy(s => s.Name).ToList();
                    availableSteps.Sort();

                    // Need to account for "in progress" steps.
                    foreach (int index in workGroup.BusyWorkers
                             .Select(worker => availableSteps.FindIndex(s => s.Name == worker.WorkingOn.Name))
                             .Where(index => index >= 0))
                    {
                        availableSteps.RemoveAt(index);
                    }
                }

                foreach (Worker worker in workGroup.AvailableWorkers.Where(worker => availableSteps.Any()))
                {
                    worker.AssignStep(availableSteps.First());
                    availableSteps.RemoveAt(0);
                }

                totalTime++;
            }

            // Correct answer: 973
            Console.WriteLine($"Total time: {totalTime}s");
        }
Beispiel #2
0
 private static void Part1(DirectedGraph graph)
 {
     // Correct answer: BCADPVTJFZNRWXHEKSQLUYGMIO
     Console.WriteLine($"Step Order: {graph.GetStepOrder()}");
 }