Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CubicArtillery
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class StartUp
- {
- public static void Main()
- {
- int bunkersCapacities = int.Parse(Console.ReadLine());
- StringBuilder result = new StringBuilder();
- Queue<Bunker> allBunkers = new Queue<Bunker>();
- Queue<int> weaponsToBeStored = new Queue<int>();
- string inputLine;
- while ((inputLine = Console.ReadLine()) != "Bunker Revision")
- {
- string[] tokens = inputLine.Split(' ');
- foreach (string token in tokens)
- {
- int currentWeapon;
- if (int.TryParse(token, out currentWeapon))
- {
- int weapon = currentWeapon;
- bool weaponIsStored = false;
- bool onlyOneBunkerIsLeft = false;
- while (!weaponIsStored && !onlyOneBunkerIsLeft)
- {
- Bunker bunker = allBunkers.Peek();
- if (allBunkers.Count == 1)
- {
- //try force store the weapon
- //since it is the only bunker it does not matter if it succeeds or not
- //we just move on to the next weapon and in order to do that we set the bool value to true
- bunker.TryForceStoreWeapon(weapon);
- onlyOneBunkerIsLeft = true;
- }
- else if (!bunker.TryStoreWeapon(weapon))
- {
- //the weapon is NOT stored successfully
- //the bunker overflows and due to that reason it gets removed
- allBunkers.Dequeue();
- //if the bunkekr is empty print empty otherwise get all the weapons joined
- string weaponsStored = bunker.WeaponsStored.Count == 0 ? "Empty" : string.Join(", ", bunker.WeaponsStored);
- result.AppendLine($"{bunker.Name} -> {weaponsStored}");
- }
- else
- {
- //the weapon is stored successfully so we have to move on to the next weapon
- //in order to do that we have to set the bool value to true
- weaponIsStored = true;
- }
- }
- }
- else
- {
- allBunkers.Enqueue(new Bunker(token, bunkersCapacities));
- }
- }
- }
- Console.Write(result);
- }
- }
- public class Bunker
- {
- private string name;
- private int totalCapacity;
- private int currentCapacity;
- private Queue<int> weaponsStored;
- public Bunker(string name, int capacity)
- {
- this.name = name;
- this.totalCapacity = capacity;
- this.currentCapacity = capacity;
- this.weaponsStored = new Queue<int>();
- }
- public string Name
- {
- get { return this.name; }
- }
- public int Capacity
- {
- get { return this.currentCapacity; }
- }
- public Queue<int> WeaponsStored
- {
- get { return this.weaponsStored; }
- }
- public bool TryStoreWeapon(int weaponToStore)
- {
- bool isStoredSuccessfully = false;
- if (this.currentCapacity >= weaponToStore)
- {
- this.StoreWeaponInBunker(weaponToStore);
- isStoredSuccessfully = true;
- }
- return isStoredSuccessfully;
- }
- public void TryForceStoreWeapon(int weaponToForceStore)
- {
- if (this.totalCapacity < weaponToForceStore)
- {
- return;
- }
- while (this.currentCapacity < weaponToForceStore)
- {
- this.currentCapacity += this.weaponsStored.Dequeue();
- }
- this.StoreWeaponInBunker(weaponToForceStore);
- }
- private void StoreWeaponInBunker(int weaponToStore)
- {
- this.weaponsStored.Enqueue(weaponToStore);
- this.currentCapacity -= weaponToStore;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement