Advertisement
simonradev

asd

Jun 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. namespace CubicArtillery
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.  
  9.     public class StartUp
  10.     {
  11.         public static void Main()
  12.         {
  13.             int bunkersCapacities = int.Parse(Console.ReadLine());
  14.  
  15.             StringBuilder result = new StringBuilder();
  16.  
  17.             Queue<Bunker> allBunkers = new Queue<Bunker>();
  18.             Queue<int> weaponsToBeStored = new Queue<int>();
  19.  
  20.             string inputLine;
  21.             while ((inputLine = Console.ReadLine()) != "Bunker Revision")
  22.             {
  23.                 string[] tokens = inputLine.Split(' ');
  24.  
  25.                 foreach (string token in tokens)
  26.                 {
  27.                     int currentWeapon;
  28.                     if (int.TryParse(token, out currentWeapon))
  29.                     {
  30.                         int weapon = currentWeapon;
  31.                         bool weaponIsStored = false;
  32.                         bool onlyOneBunkerIsLeft = false;
  33.                         while (!weaponIsStored && !onlyOneBunkerIsLeft)
  34.                         {
  35.                             Bunker bunker = allBunkers.Peek();
  36.  
  37.                             if (allBunkers.Count == 1)
  38.                             {
  39.                                 //try force store the weapon
  40.                                 //since it is the only bunker it does not matter if it succeeds or not
  41.                                 //we just move on to the next weapon and in order to do that we set the bool value to true
  42.                                 bunker.TryForceStoreWeapon(weapon);
  43.  
  44.                                 onlyOneBunkerIsLeft = true;
  45.                             }
  46.                             else if (!bunker.TryStoreWeapon(weapon))
  47.                             {
  48.                                 //the weapon is NOT stored successfully
  49.                                 //the bunker overflows and due to that reason it gets removed
  50.                                 allBunkers.Dequeue();
  51.  
  52.                                 //if the bunkekr is empty print empty otherwise get all the weapons joined
  53.                                 string weaponsStored = bunker.WeaponsStored.Count == 0 ? "Empty" : string.Join(", ", bunker.WeaponsStored);
  54.                                 result.AppendLine($"{bunker.Name} -> {weaponsStored}");
  55.                             }
  56.                             else
  57.                             {
  58.                                 //the weapon is stored successfully so we have to move on to the next weapon
  59.                                 //in order to do that we have to set the bool value to true
  60.                                 weaponIsStored = true;
  61.                             }
  62.                         }
  63.                     }
  64.                     else
  65.                     {
  66.                         allBunkers.Enqueue(new Bunker(token, bunkersCapacities));
  67.                     }
  68.                 }
  69.                
  70.             }
  71.  
  72.             Console.Write(result);
  73.         }
  74.     }
  75.  
  76.     public class Bunker
  77.     {
  78.         private string name;
  79.         private int totalCapacity;
  80.         private int currentCapacity;
  81.         private Queue<int> weaponsStored;
  82.  
  83.         public Bunker(string name, int capacity)
  84.         {
  85.             this.name = name;
  86.             this.totalCapacity = capacity;
  87.             this.currentCapacity = capacity;
  88.             this.weaponsStored = new Queue<int>();
  89.         }
  90.  
  91.         public string Name
  92.         {
  93.             get { return this.name; }
  94.         }
  95.  
  96.         public int Capacity
  97.         {
  98.             get { return this.currentCapacity; }
  99.         }
  100.  
  101.         public Queue<int> WeaponsStored
  102.         {
  103.             get { return this.weaponsStored; }
  104.         }
  105.  
  106.         public bool TryStoreWeapon(int weaponToStore)
  107.         {
  108.             bool isStoredSuccessfully = false;
  109.             if (this.currentCapacity >= weaponToStore)
  110.             {
  111.                 this.StoreWeaponInBunker(weaponToStore);
  112.                 isStoredSuccessfully = true;
  113.             }
  114.  
  115.             return isStoredSuccessfully;
  116.         }
  117.  
  118.         public void TryForceStoreWeapon(int weaponToForceStore)
  119.         {
  120.             if (this.totalCapacity < weaponToForceStore)
  121.             {
  122.                 return;
  123.             }
  124.  
  125.             while (this.currentCapacity < weaponToForceStore)
  126.             {
  127.                 this.currentCapacity += this.weaponsStored.Dequeue();
  128.             }
  129.  
  130.             this.StoreWeaponInBunker(weaponToForceStore);
  131.         }
  132.  
  133.         private void StoreWeaponInBunker(int weaponToStore)
  134.         {
  135.             this.weaponsStored.Enqueue(weaponToStore);
  136.             this.currentCapacity -= weaponToStore;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement