Advertisement
simonradev

CubicArtierlly90/100

Jun 18th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 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 Program
  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.                         weaponsToBeStored.Enqueue(currentWeapon);
  31.                     }
  32.                     else
  33.                     {
  34.                         allBunkers.Enqueue(new Bunker(token, bunkersCapacities));
  35.                     }
  36.                 }
  37.  
  38.                 while (weaponsToBeStored.Count > 0)
  39.                 {
  40.                     int weapon = weaponsToBeStored.Dequeue();
  41.  
  42.                     bool weaponIsStored = false;
  43.                     bool onlyOneBunkerIsLeft = false;
  44.                     while (!weaponIsStored && !onlyOneBunkerIsLeft)
  45.                     {
  46.                         Bunker bunker = allBunkers.Peek();
  47.  
  48.                         if (allBunkers.Count == 1)
  49.                         {
  50.                             //try force store the weapon
  51.                             //since it is the only bunker it does not matter if it succeeds or not
  52.                             //we just move on to the next weapon and in order to do that we set the bool value to true
  53.                             bunker.TryForceStoreWeapon(weapon);
  54.  
  55.                             onlyOneBunkerIsLeft = true;
  56.                         }
  57.                         else if (!bunker.TryStoreWeapon(weapon))
  58.                         {
  59.                             //the weapon is NOT stored successfully
  60.                             //the bunker overflows and due to that reason it gets removed
  61.                             allBunkers.Dequeue();
  62.  
  63.                             //if the bunkekr is empty print empty otherwise get all the weapons joined
  64.                             string weaponsStored = bunker.WeaponsStored.Count == 0 ? "Empty" : string.Join(", ", bunker.WeaponsStored);
  65.                             result.AppendLine($"{bunker.Name} -> {weaponsStored}");
  66.                         }
  67.                         else
  68.                         {
  69.                             //the weapon is stored successfully so we have to move on to the next weapon
  70.                             //in order to do that we have to set the bool value to true
  71.                             weaponIsStored = true;
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             Console.Write(result);
  78.         }
  79.     }
  80.  
  81.     public class Bunker
  82.     {
  83.         private string name;
  84.         private int totalCapacity;
  85.         private int currentCapacity;
  86.         private Queue<int> weaponsStored;
  87.  
  88.         public Bunker(string name, int capacity)
  89.         {
  90.             this.name = name;
  91.             this.totalCapacity = capacity;
  92.             this.currentCapacity = capacity;
  93.             this.weaponsStored = new Queue<int>();
  94.         }
  95.  
  96.         public string Name
  97.         {
  98.             get { return this.name; }
  99.         }
  100.  
  101.         public int Capacity
  102.         {
  103.             get { return this.currentCapacity; }
  104.         }
  105.  
  106.         public Queue<int> WeaponsStored
  107.         {
  108.             get { return this.weaponsStored; }
  109.         }
  110.  
  111.         public bool TryStoreWeapon(int weaponToStore)
  112.         {
  113.             bool isStoredSuccessfully = false;
  114.             if (this.currentCapacity >= weaponToStore)
  115.             {
  116.                 this.currentCapacity -= weaponToStore;
  117.                 this.weaponsStored.Enqueue(weaponToStore);
  118.                 isStoredSuccessfully = true;
  119.             }
  120.  
  121.             return isStoredSuccessfully;
  122.         }
  123.  
  124.         public void TryForceStoreWeapon(int weaponToForceStore)
  125.         {
  126.             if (this.totalCapacity < weaponToForceStore)
  127.             {
  128.                 return;
  129.             }
  130.  
  131.             while (this.currentCapacity < weaponToForceStore)
  132.             {
  133.                 this.currentCapacity += this.weaponsStored.Dequeue();
  134.             }
  135.  
  136.             this.weaponsStored.Enqueue(weaponToForceStore);
  137.             this.currentCapacity -= weaponToForceStore;
  138.  
  139.             return;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement