Advertisement
Danny_Berova

03.GreedyTimes

Feb 4th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.GreedyTimes
  6. {
  7.     public class GreedyTimes
  8.     {
  9.         public static void Main()
  10.         {
  11.             var types = new Dictionary<string, UnitType>();
  12.             types.Add("Gold", new UnitType("Gold"));
  13.             types.Add("Gem", new UnitType("Gem"));
  14.             types.Add("Cash", new UnitType("Cash"));
  15.  
  16.            
  17.  
  18.             var capacity = long.Parse(Console.ReadLine());
  19.             var inputLine = Console.ReadLine();
  20.             var inputTokens = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  21.  
  22.             for (int i = 0; i < inputTokens.Length - 1; i += 2)
  23.             {
  24.                 var itemCounter = types.Sum(x => x.Value.Units.Count());
  25.                 if (itemCounter >= capacity)
  26.                 {
  27.                     break;
  28.                 }
  29.  
  30.                 var tokenToChars = inputTokens[i].ToCharArray();
  31.                 var currentUnitName = inputTokens[i];
  32.                 var currentUnitAmount = long.Parse(inputTokens[i + 1]);
  33.                 var currentUnit = new Unit(currentUnitName, currentUnitAmount);
  34.  
  35.  
  36.  
  37.                 if (inputTokens[i].ToLower() == "gold")
  38.                 {
  39.                     if (types["Gold"].Units == null || !types["Gold"].Units.Any(x => x.Name == currentUnitName))
  40.                     {
  41.                         types["Gold"].Units.Add(currentUnit);
  42.                         types["Gold"].TotalAmount += currentUnitAmount;
  43.  
  44.                         continue;
  45.                     }
  46.                     types["Gold"].Units.First(x => x.Name == currentUnitName).Amount += currentUnitAmount;
  47.                     types["Gold"].TotalAmount += currentUnitAmount;
  48.  
  49.                 }
  50.                 if (inputTokens[i].ToLower().EndsWith("gem") && inputTokens[i].Length >= 4)
  51.                 {
  52.  
  53.                     if (types["Gold"].TotalAmount >= types["Gem"].TotalAmount + currentUnitAmount)
  54.                     {
  55.                         if (types["Gem"].Units == null || !types["Gem"].Units.Any(x => x.Name == currentUnitName))
  56.                         {
  57.                             types["Gem"].Units.Add(currentUnit);
  58.                             types["Gem"].TotalAmount += currentUnitAmount;
  59.                             continue;
  60.                         }
  61.                         types["Gem"].Units.First(x => x.Name == currentUnitName).Amount += currentUnitAmount;
  62.                         types["Gem"].TotalAmount += currentUnitAmount;
  63.                     }
  64.                 }
  65.                 if (currentUnitName.Length == 3 && tokenToChars.All(x => char.IsLetter(x)))
  66.                 {
  67.                     if (types["Gem"].TotalAmount >= types["Cash"].TotalAmount + currentUnitAmount)
  68.                     {
  69.                         if (types["Cash"].Units == null || !types["Cash"].Units.Any(x => x.Name == currentUnitName))
  70.                         {
  71.                             types["Cash"].Units.Add(currentUnit);
  72.                             types["Cash"].TotalAmount += currentUnitAmount;
  73.                             continue;
  74.                         }
  75.                         types["Cash"].Units.First(x => x.Name == currentUnitName).Amount += currentUnitAmount;
  76.                         types["Cash"].TotalAmount += currentUnitAmount;
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             foreach (var type in types.Where(x => x.Value.TotalAmount > 0).OrderByDescending(x => x.Value.TotalAmount))
  82.             {
  83.                 Console.WriteLine($"<{type.Key}> ${type.Value.TotalAmount}");
  84.                 foreach (var unit in type.Value.Units.OrderByDescending(x => x.Name).ThenBy(x => x.Amount))
  85.                 {
  86.                     Console.WriteLine($"##{unit.Name} - {unit.Amount}");
  87.                 }
  88.             }
  89.  
  90.  
  91.  
  92.         }
  93.  
  94.         public class Unit
  95.         {
  96.             public Unit()
  97.             {
  98.  
  99.             }
  100.             public Unit(string name, long amount)
  101.             {
  102.                 this.Name = name;
  103.                 this.Amount = amount;
  104.             }
  105.             public string Name { get; set; }
  106.             public long Amount { get; set; }
  107.         }
  108.  
  109.         public class UnitType
  110.         {
  111.            
  112.             public UnitType(string name)
  113.             {
  114.                 this.Name = name;
  115.                 this.Units = new List<Unit>();
  116.             }
  117.             public string Name { get; set; }
  118.             public long TotalAmount { get; set; }
  119.             public List<Unit> Units { get; set; }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement