Advertisement
Danny_Berova

03.GreedyTimes

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