Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. namespace _04.CubicAssault
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text.RegularExpressions;
  7.  
  8.     public static class CubicAssault
  9.     {
  10.         private const string End = "Count em all";
  11.         private const int ValueBreakpoint = 1000000;
  12.         private const string BestType = "Black";
  13.  
  14.         private static readonly SortedDictionary<string, SortedDictionary<string, long>> Statistics =
  15.             new SortedDictionary<string, SortedDictionary<string, long>>();
  16.  
  17.         internal enum SoldierType
  18.         {
  19.             Green = 0,
  20.             Red = 1,
  21.             Black = 2
  22.         }
  23.  
  24.         public static void Main()
  25.         {
  26.             var input = Console.ReadLine();
  27.             while (input != End)
  28.             {
  29.                 var tokens = Regex.Split(input, @"\s+\-\>\s+");
  30.  
  31.                 var regionName = tokens[0];
  32.                 var soldierType = tokens[1];
  33.                 var soldiersCount = long.Parse(tokens[2]);
  34.  
  35.                 if (!Statistics.ContainsKey(regionName))
  36.                 {
  37.                     Statistics[regionName] = new SortedDictionary<string, long>();
  38.                     for (int i = 0; i < Enum.GetNames(typeof(SoldierType)).Length; i++)
  39.                     {
  40.                         Statistics[regionName].Add(((SoldierType)i).ToString(), 0);
  41.                     }
  42.                 }
  43.  
  44.                 RecalculateValues(regionName, soldierType, soldiersCount);
  45.  
  46.                 input = Console.ReadLine();
  47.             }
  48.  
  49.             SortAndPrint();
  50.         }
  51.  
  52.         private static void SortAndPrint()
  53.         {
  54.             var regions = Statistics
  55.                 .OrderByDescending(i => i.Value[BestType])
  56.                 .ThenBy(item => item.Key.Length);
  57.  
  58.             foreach (var region in regions)
  59.             {
  60.                 Console.WriteLine(region.Key);
  61.  
  62.                 var stats = region.Value
  63.                     .OrderByDescending(t => t.Value);
  64.  
  65.                 foreach (var stat in stats)
  66.                 {
  67.                     Console.WriteLine($"-> {stat.Key} : {stat.Value}");
  68.                 }
  69.             }
  70.         }
  71.  
  72.         private static void RecalculateValues(string regionName, string soldierType, long soldiersCount)
  73.         {
  74.             while (true)
  75.             {
  76.                 var totalCount = Statistics[regionName][soldierType] + soldiersCount;
  77.  
  78.                 if (soldierType == SoldierType.Black.ToString() ||
  79.                     totalCount < ValueBreakpoint)
  80.                 {
  81.                     // All good - increase the value
  82.                     Statistics[regionName][soldierType] = totalCount;
  83.                     break;
  84.                 }
  85.  
  86.                 var newValue = totalCount % ValueBreakpoint;
  87.                 soldiersCount = (totalCount - newValue) / ValueBreakpoint;
  88.  
  89.                 var currentTypeIndex = (int)Enum.Parse(typeof(SoldierType), soldierType);
  90.                 Statistics[regionName][((SoldierType)currentTypeIndex).ToString()] = newValue;
  91.                 soldierType = ((SoldierType)currentTypeIndex + 1).ToString();
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement