Esempio n. 1
0
        private static Event HandleLoadCommand(DronesInput input, LoadCommand loadCommand, Dictionary <Product, int> carriedProducts,
                                               Drone drone, ref Coordinate droneLocation, ref long currentTurn, ref long carriedWeight)
        {
            var distance = droneLocation.CalcEucledianDistance(loadCommand.Warehouse.Location);

            currentTurn   += ((int)Math.Ceiling(distance)) + 1;
            droneLocation  = loadCommand.Warehouse.Location;
            carriedWeight += loadCommand.Product.Weight * loadCommand.ProductCount;
            carriedProducts[loadCommand.Product] = carriedProducts.GetOrDefault(loadCommand.Product, 0) + loadCommand.ProductCount;

            if (carriedWeight > input.MaxWeight)
            {
                throw new Exception(string.Format("Drone {0} is carrying {1} weight in turn {2}, which is more than maximum ({3})",
                                                  drone.Index, carriedWeight, currentTurn, input.MaxWeight));
            }

            var ev = new Event
            {
                Turn         = currentTurn,
                Warehouse    = loadCommand.Warehouse,
                ProductTaken = loadCommand.Product,
                TakenCount   = loadCommand.ProductCount,
                Drone        = drone
            };

            return(ev);
        }
Esempio n. 2
0
        private static Event HandleUnloadCommand(UnloadCommand unloadCommand, Dictionary <Product, int> carriedProducts, Drone drone,
                                                 ref Coordinate droneLocation, ref long currentTurn, ref long carriedWeight)
        {
            var distance = droneLocation.CalcEucledianDistance(unloadCommand.Warehouse.Location);

            currentTurn   += ((int)Math.Ceiling(distance)) + 1;
            droneLocation  = unloadCommand.Warehouse.Location;
            carriedWeight -= unloadCommand.Product.Weight * unloadCommand.ProductCount;
            var newCount = carriedProducts.GetOrDefault(unloadCommand.Product, 0) - unloadCommand.ProductCount;

            if (newCount < 0)
            {
                throw new Exception(string.Format("Drone {0} attempted to unload {1} products of type {2} which he doesn't have",
                                                  drone.Index, unloadCommand.ProductCount, unloadCommand.Product.Index));
            }

            carriedProducts[unloadCommand.Product] = newCount;

            var ev = new Event
            {
                Turn             = currentTurn,
                Warehouse        = unloadCommand.Warehouse,
                ProductDelivered = unloadCommand.Product,
                DeliveredCount   = unloadCommand.ProductCount,
                Drone            = drone
            };

            return(ev);
        }