Ejemplo n.º 1
0
        public void GetAnswerB()
        {
            Console.WriteLine("Answer B: ");
            ParseInput(path);

            var orderedSteps = steps.OrderBy(x => x.Key).ToList();

            Worker[] workers = new Worker[5];

            for (int i = 0; i < workers.Length; i++)
            {
                workers[i] = new Worker();
            }

            int time = 0;

            bool waitFlag = false;

            while (orderedSteps.Count > 0)
            {
                for (int j = 0; j < workers.Length; j++)
                {
                    Worker w = workers[j];
                    if (w.step == null && !waitFlag)
                    {
                        //find the next step to work on
                        w.GetStep(orderedSteps);
                    }

                    if (w.step != null)
                    {
                        //process step
                        w.step.stepTime--;
                        //Console.Write(string.Format("{0} ", w.step.stepId));

                        if (w.step.stepTime < 1)
                        {
                            for (int i = 0; i < orderedSteps.Count; i++)
                            {
                                if (orderedSteps[i].Value.stepPrerequisites.Contains(w.step.stepId))
                                {
                                    requirementCount--;
                                }

                                orderedSteps[i].Value.stepPrerequisites.Remove(w.step.stepId);
                            }

                            w.step = null;
                            w.GetStep(orderedSteps);
                            waitFlag = true;
                        }
                    }
                    //else
                    //{
                    //    Console.Write("- ");
                    //}
                }
                time++;
                //Console.Write("\n");
                waitFlag = false;
            }

            //get any remaining time after initial calculate, because it ends before allowing workers to finish their task
            foreach (Worker w in workers)
            {
                while (w.step != null)
                {
                    time++;
                    w.step.stepTime--;
                    //Console.WriteLine(string.Format("{0} ", w.step.stepId));
                    if (w.step.stepTime < 1)
                    {
                        w.step = null;
                    }
                }
            }

            Console.WriteLine("Time: " + time);
        }