Ejemplo n.º 1
0
        private ToyOrder PickLargestCompletableToday(DateTime nextAvailable)
        {
            int bestBracket = (int)Math.Floor((double)effectiveWorkTimeLeft / ToyOrderBook.orderBracketsQuotient);

            ToyOrder toy = null;

            for (int i = bestBracket; i >= 0; i--)
            {
                List <ToyOrder> bracket = ToyOrderBook.orderLists [i];

                if (bracket.Count > 0)
                {
                    toy = PickAvailableToyInBracket(bracket, nextAvailable);
                }

                if (toy != null)
                {
                    break;
                }
            }

            if (toy == null)
            {
                toy = PickSmallestAvailableToy(nextAvailable);
            }

            return(toy);
        }
Ejemplo n.º 2
0
        public ToyOrder ChooseToy()
        {
            ToyOrder toy = null;

            DateTime nextAvailable = Hours.Duplicate(this.nextAvailable);

            double highSkilledCutoff = 3.8;
            bool   isProductivityCappedElfAtStartOfDay = this.productivity >= highSkilledCutoff && nextAvailable.Hour <= 12;

            if (isProductivityCappedElfAtStartOfDay)
            {
                toy = PickLargestAvailableToy(nextAvailable);
            }
            else if (this.productivity < highSkilledCutoff)
            {
                toy = PickSmallestAvailableToy(nextAvailable);
            }
            else
            {
                toy = PickLargestCompletableToday(nextAvailable);
            }

            if (toy == null)
            {
                toy = PickErliestAvailableToy();
            }

            return(toy);
        }
        private static int FilterNewOrderIntoBracket(ToyOrder toyOrder)
        {
            int listIndex = CalculateBracketNumber(toyOrder);

            orderLists [listIndex].Add(toyOrder);

            return(listIndex);
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Start of program. Press any key....");
            Console.ReadKey();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();


            try {
                ToyOrderBook.SetupOrderLists();

                DataReader.OpenReadStream(DataReader.filePath);
                Console.WriteLine(DataReader.ReadHeaderLine());
                ToyOrderBook.AddNewOrdersToOrderBook(DataReader.ReadLinesFromStream(N_TOYS));
                DataReader.CloseReadStream();

                ElfCoordinator.HireElves(N_ELVES);

                int remainingOrders = ToyOrderBook.CountAllOrdersInBook();
                int processedOrders = 0;
                while (remainingOrders > 0)
                {
                    processedOrders++;

                    bool shouldPrint = processedOrders % 1000 == 0;
                    if (shouldPrint)
                    {
                        PrintProgress(processedOrders);
                    }

                    Elf      elf = ElfCoordinator.PickNextElf();
                    ToyOrder toy = elf.ChooseToy();
                    elf.BuildToy(toy);

                    remainingOrders = ToyOrderBook.CountAllOrdersInBook();
                }

                ToyOrderBook.PrintCompletedOrdersToConsole();
                Console.WriteLine(String.Format("Completed Toys: {0}", ToyOrderBook.completedOrders.Count));
                ToyOrder lastToy = ToyOrderBook.lastOrderCompleted;
                Console.WriteLine(String.Format("Last Toy Completed: Toy {0} completed at {1} {2}", lastToy.iD, lastToy.finishTime.ToShortDateString(), lastToy.finishTime.ToShortTimeString()));
                Console.WriteLine(String.Format("Total Minutes: {0}", CalculateTotalMinutes()));
                Console.WriteLine(String.Format("Score: {0}", CalculateScore()));

                WriteResultsFile();
            } catch (Exception ex) {
                Console.WriteLine("In 'Main()'");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ToString());
            }

            stopwatch.Stop();
            Console.WriteLine(String.Format("Elapsed Time: {0}", stopwatch.Elapsed.ToString()));
            Console.WriteLine("End of program.");
        }
Ejemplo n.º 5
0
        private ToyOrder PickErliestAvailableToy()
        {
            ToyOrder        toy = null;
            List <ToyOrder> earliestToysInSubDayBrackets   = new List <ToyOrder> ();
            List <ToyOrder> earliestToysInSuperDayBrackets = new List <ToyOrder> ();

            for (int i = 0; i <= Math.Min(effectiveWorkTimeLeft, ToyOrderBook.orderBracketsCount - 1); i++)
            {
                List <ToyOrder> bracket = ToyOrderBook.orderLists [i];
                if (bracket.Count > 0)
                {
                    earliestToysInSubDayBrackets.Add(bracket [0]);
                }
            }

            for (int i = Math.Min(effectiveWorkTimeLeft + 1, ToyOrderBook.orderBracketsCount); i < ToyOrderBook.orderLists.Count; i++)
            {
                List <ToyOrder> bracket = ToyOrderBook.orderLists [i];
                if (bracket.Count > 0)
                {
                    earliestToysInSuperDayBrackets.Add(bracket [0]);
                }
            }

            ToyOrder earliestSubDayToy = null;

            if (earliestToysInSubDayBrackets.Count > 0)
            {
                earliestSubDayToy = earliestToysInSubDayBrackets.OrderBy(x => x.arrivalTime).ToList() [0];
            }
            ToyOrder earliestSuperDayToy = null;

            if (earliestToysInSuperDayBrackets.Count > 0)
            {
                earliestSuperDayToy = earliestToysInSuperDayBrackets.OrderBy(x => x.arrivalTime).ToList() [0];
            }

            if (earliestSubDayToy == null)
            {
                toy = earliestSuperDayToy;
            }
            else if (earliestSuperDayToy == null)
            {
                toy = earliestSubDayToy;
            }
            else if ((earliestSubDayToy.arrivalTime - earliestSuperDayToy.arrivalTime).TotalDays > 1)
            {
                toy = earliestSuperDayToy;
            }
            else
            {
                toy = earliestSubDayToy;
            }

            return(toy);
        }
Ejemplo n.º 6
0
        private ToyOrder PickAvailableToyInBracket(List <ToyOrder> bracket, DateTime nextAvailable)
        {
            ToyOrder toy = null;
            ToyOrder firstToyInBracket = bracket [0];

            if (firstToyInBracket.arrivalTime <= nextAvailable)
            {
                toy = firstToyInBracket;
            }
            return(toy);
        }
        public static bool RemoveOrderFromBooks(ToyOrder order)
        {
            bool isOrderRemoved = false;

            int bracketNumber = CalculateBracketNumber(order);

            if (orderLists [bracketNumber].Count > 0)
            {
                isOrderRemoved = orderLists [bracketNumber].Remove(order);
            }
            return(isOrderRemoved);
        }
        public static int AddNewOrdersToOrderBook(List <string> incomingOrders)
        {
            int ordersAdded = 0;

            foreach (string order in incomingOrders)
            {
                ToyOrder newOrder  = new ToyOrder(order);
                int      listIndex = FilterNewOrderIntoBracket(newOrder);
                if (listIndex > -1)
                {
                    ordersAdded++;
                }
            }
            return(ordersAdded);
        }
        private static int CalculateBracketNumber(ToyOrder toyOrder)
        {
            int bracketNumber = 0;

            if (toyOrder.durationMinutes < MAX_ORDER_DURATION_MINUTES)
            {
                bracketNumber = (int)Math.Floor((double)toyOrder.durationMinutes / orderBracketsQuotient);
            }
            else
            {
                bracketNumber = ToyOrderBook.orderBracketsCount;
            }

            return(bracketNumber);
        }
Ejemplo n.º 10
0
        DateTime CalculateStartTime(ToyOrder toy, int minutesTillFinished)
        {
            DateTime startTime;
            DateTime earliestAvailableTime = new DateTime(Math.Max(toy.arrivalTime.Ticks, this.nextAvailable.Ticks));

            startTime = Hours.NextSanctionedMinute(earliestAvailableTime);

            bool isLessThanFullDayToy = minutesTillFinished < 600;

            int    minutesLeftInDay           = Hours.MinutesLeftInWorkday(startTime);
            double overtimeFractionForbidden  = 1 - Math.Min(Math.Pow(MainClass.CalculateFractionComplete(), 5), 0.15);
            bool   isOutsideOvertimeTolerance = (minutesTillFinished * overtimeFractionForbidden) > minutesLeftInDay;

            if (isLessThanFullDayToy && isOutsideOvertimeTolerance)
            {
                startTime = Hours.NextMorning(startTime);
            }
            return(startTime);
        }
Ejemplo n.º 11
0
        private ToyOrder PickSmallestAvailableToy(DateTime nextAvailable)
        {
            ToyOrder toy = null;

            for (int i = 0; i < ToyOrderBook.orderLists.Count; i++)
            {
                List <ToyOrder> bracket = ToyOrderBook.orderLists [i];

                if (bracket.Count > 0)
                {
                    toy = PickAvailableToyInBracket(bracket, nextAvailable);
                }

                if (toy != null)
                {
                    break;
                }
            }

            return(toy);
        }
Ejemplo n.º 12
0
        public void BuildToy(ToyOrder toy)
        {
            int minutesTillFinished = (int)Math.Ceiling((double)toy.durationMinutes / this.productivity);

            DateTime startTime = CalculateStartTime(toy, minutesTillFinished);

            this.workingTill = startTime.AddMinutes(minutesTillFinished);

            int sanctionedMinutes   = Hours.CalculateSanctionedMinutes(startTime, minutesTillFinished);
            int unsanctionedMinutes = minutesTillFinished - sanctionedMinutes;

            this.neededRest = unsanctionedMinutes;

            toy.elfId         = this.id;
            toy.startTime     = startTime;
            toy.finalDuration = minutesTillFinished;

            UpdateProductivity(sanctionedMinutes, unsanctionedMinutes);

            ToyOrderBook.completedOrders.Add(toy);
            ToyOrderBook.RemoveOrderFromBooks(toy);
        }
 public static void PrintCompletedOrdersToConsole(int maxToPrint = 25)
 {
     if (completedOrders.Count <= maxToPrint)
     {
         foreach (ToyOrder toy in completedOrders)
         {
             Console.WriteLine(toy.ToString());
         }
     }
     else
     {
         for (int i = 0; i < 10; i++)
         {
             ToyOrder toy = completedOrders [i];
             Console.WriteLine(toy.ToString());
         }
         Console.WriteLine("---");
         for (int j = completedOrders.Count - 1 - 10; j < completedOrders.Count; j++)
         {
             ToyOrder toy = completedOrders [j];
             Console.WriteLine(toy.ToString());
         }
     }
 }