Advertisement
shady_obeyd

03.GreedyTimes

Feb 8th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.GreedyTimes
  6. {
  7.     class GreedyTimes
  8.     {
  9.         static void Main()
  10.         {
  11.             Dictionary<string, long> goldData = new Dictionary<string, long>();
  12.             long goldQuantity = 0;
  13.             Dictionary<string, long> gemsData = new Dictionary<string, long>();
  14.             long gemsQuantity = 0;
  15.             Dictionary<string, long> cashData = new Dictionary<string, long>();
  16.             long cashQuantity = 0;
  17.  
  18.             long bagLimit = long.Parse(Console.ReadLine());
  19.  
  20.             string[] itemsInput = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.             for (int i = 0; i < itemsInput.Length; i += 2)
  23.             {
  24.                 long bagSize = goldQuantity + gemsQuantity + cashQuantity;
  25.  
  26.                 string itemName = itemsInput[i];
  27.                 long quantity = long.Parse(itemsInput[i + 1]);
  28.  
  29.                 if (bagSize >= bagLimit)
  30.                 {
  31.                     break;
  32.                 }
  33.  
  34.                 if (itemName.Length == 3)
  35.                 {
  36.                     cashQuantity += quantity;
  37.  
  38.                     if (gemsQuantity >= cashQuantity)
  39.                     {
  40.                         FillDictionary(cashData, itemName, quantity);
  41.                     }
  42.                     else
  43.                     {
  44.                         cashQuantity -= quantity;
  45.                     }
  46.                 }
  47.                 else if (itemName.ToLower().EndsWith("gem"))
  48.                 {
  49.                     gemsQuantity += quantity;
  50.  
  51.                     if (goldQuantity >= gemsQuantity)
  52.                     {
  53.                         FillDictionary(gemsData, itemName, quantity);
  54.                     }
  55.                     else
  56.                     {
  57.                         gemsQuantity -= quantity;
  58.                     }
  59.                 }
  60.                 else if (itemName.ToLower() == "gold")
  61.                 {
  62.                     goldQuantity += quantity;
  63.                     FillDictionary(goldData, itemName, quantity);
  64.                 }
  65.             }
  66.  
  67.             if (goldData.Any())
  68.             {
  69.                 PrintBag(goldData, "Gold");
  70.             }
  71.  
  72.             if (gemsData.Any())
  73.             {
  74.                 PrintBag(gemsData, "Gem");
  75.             }
  76.  
  77.             if (cashData.Any())
  78.             {
  79.                 PrintBag(cashData, "Cash");
  80.             }
  81.         }
  82.  
  83.         static void PrintBag(Dictionary<string, long> bag, string type)
  84.         {
  85.             Console.WriteLine($"<{type}> ${bag.Values.Sum()}");
  86.  
  87.             foreach (KeyValuePair<string, long> itemData in bag.OrderByDescending(n => n.Key).ThenBy(a => a.Value))
  88.             {
  89.                 string item = itemData.Key;
  90.                 long quantity = itemData.Value;
  91.  
  92.                 Console.WriteLine($"##{item} - {quantity}");
  93.             }
  94.         }
  95.  
  96.         static void FillDictionary(Dictionary<string, long> bag, string itemName, long quantity)
  97.         {
  98.             if (!bag.ContainsKey(itemName))
  99.             {
  100.                 bag.Add(itemName, quantity);
  101.             }
  102.             else
  103.             {
  104.                 bag[itemName] += quantity;
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement