Exemple #1
0
        private void ComputeLevel5(string[] carDefs)
        {
            LinkedList <int> carsQueue = new LinkedList <int>();
            List <ParkedCar> parking   = new List <ParkedCar>();

            List <ParkeingSpot> parkingSpots = GetParkingSpots(carDefs[1]);
            List <int>          kilosTable   = GetKilosTable(carDefs[2]);

            double markFortune = 0;

            foreach (string carDef in carDefs[3].Trim().Split(' '))
            {
                int car = Int32.Parse(carDef);

                if (car > 0 && parkingSpots.Count > 0)
                {
                    ParkeingSpot firstFree = parkingSpots.First();
                    parkingSpots.Remove(firstFree);
                    parking.Add(new ParkedCar {
                        carNumber = car, spot = firstFree
                    });
                }
                else if (car > 0)
                {
                    carsQueue.AddLast(car);
                }
                else if (car < 0)
                {
                    ParkedCar p = parking.FirstOrDefault(it => it.carNumber == Math.Abs(car));
                    if (p != null)
                    {
                        parking.Remove(p);
                        // the kilosTable is 0 based index cars are numerotated from 1
                        double addedValue = Math.Ceiling((double)kilosTable[p.carNumber - 1] / 100);

                        markFortune += (p.spot.spotPrice * addedValue);
                        parkingSpots.Add(p.spot);
                        parkingSpots = parkingSpots.OrderBy(it => it.spotNumber).ToList();

                        if (carsQueue.Count > 0)
                        {
                            int newCar = carsQueue.First.Value;
                            carsQueue.RemoveFirst();
                            ParkeingSpot firstFree = parkingSpots.First();
                            parkingSpots.Remove(firstFree);
                            parking.Add(new ParkedCar {
                                carNumber = newCar, spot = firstFree
                            });
                        }
                    }
                    else if (carsQueue.Count > 0)
                    {
                        carsQueue.Remove(Math.Abs(car));
                    }
                }
            }

            Console.WriteLine("Mark max:" + markFortune);
        }
Exemple #2
0
        private void ComputeLevel3(string[] carDefs)
        {
            Queue <int>      carsQueue = new Queue <int>();
            List <ParkedCar> parking   = new List <ParkedCar>();


            List <ParkeingSpot> parkingSpots = GetParkingSpots(carDefs[1]);
            int markFortune = 0;

            foreach (string carDef in carDefs[2].Trim().Split(' '))
            {
                int car = Int32.Parse(carDef);

                if (car > 0 && parkingSpots.Count > 0)
                {
                    ParkeingSpot firstFree = parkingSpots.First();
                    parkingSpots.Remove(firstFree);
                    parking.Add(new ParkedCar {
                        carNumber = car, spot = firstFree
                    });
                }
                else if (car > 0)
                {
                    carsQueue.Enqueue(car);
                }
                else if (car < 0)
                {
                    ParkedCar p = parking.First(it => it.carNumber == Math.Abs(car));
                    parking.Remove(p);
                    markFortune += p.spot.spotPrice;
                    parkingSpots.Add(p.spot);
                    parkingSpots = parkingSpots.OrderBy(it => it.spotNumber).ToList();

                    if (carsQueue.Count > 0)
                    {
                        int          newCar    = carsQueue.Dequeue();
                        ParkeingSpot firstFree = parkingSpots.First();
                        parkingSpots.Remove(firstFree);
                        parking.Add(new ParkedCar {
                            carNumber = newCar, spot = firstFree
                        });
                    }
                }
            }

            Console.WriteLine("Mark max:" + markFortune);
        }