Advertisement
dimipan80

Dragon Army

Mar 19th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. /*
  2. Heroes III is the best game ever. Everyone loves it and everyone plays it all the time.
  3. Stamat is no exclusion to this rule. His favorite units in the game are all types of dragons – black, red, gold, azure etc…
  4. He likes them so much that he gives them names and keeps logs of their stats: damage, health and armor.
  5. The process of aggregating all the data is quite tedious, so he would like to have a program doing it.
  6. You need to categorize dragons by their type. For each dragon, identified by name, keep information about his stats.
  7. Type is preserved as in the order of input, but dragons are sorted alphabetically by name.
  8. For each type, you should also print the average damage, health and armor of the dragons. For each dragon, print his own stats.
  9. There may be missing stats in the input, though. If a stat is missing you should assign it default values.
  10. Default values are as follows: health 250, damage 45, and armor 10. Missing stat will be marked by null.
  11. The input is in the following format: {type} {name} {damage} {health} {armor}. Any of the integers may be assigned null value.
  12. If the same dragon is added a second time, the new stats should overwrite the previous ones.
  13. Two dragons are considered equal if they match by both name and type.
  14. Input:
  15. On the first line, you are given number N -> the number of dragons to follow
  16. On the next N lines you are given input in the above described format. There will be single space separating each element.
  17. Output:
  18. Print the aggregated data on the console:
  19. For each type, print average stats of its dragons in format: {Type}::({damage}/{health}/{armor})
  20. Damage, health and armor should be rounded to two digits after the decimal separator.
  21. For each dragon, print its stats in format: -{Name} -> damage: {damage}, health: {health}, armor: {armor}
  22. Constraints:
  23. N is in range [1…100]
  24. The dragon type and name are one word only, starting with capital letter.
  25. Damage, health and armor are integers in range [0 … 100000] or null.
  26. */
  27.  
  28. namespace Dragon_Army
  29. {
  30.     using System;
  31.     using System.Collections.Generic;
  32.     using System.Linq;
  33.     using System.Text.RegularExpressions;
  34.  
  35.     internal static class Dragons
  36.     {
  37.         private static void Main()
  38.         {
  39.             var regex = new Regex(@"([a-zA-Z]+) ([a-zA-Z]+) (null|\d+) (null|\d+) (null|\d+)");
  40.             var typesDragons = new Dictionary<string, SortedDictionary<string, int[]>>();
  41.  
  42.             var rows = int.Parse(Console.ReadLine());
  43.             for (int i = 0; i < rows; i++)
  44.             {
  45.                 var inputLine = Console.ReadLine();
  46.                 var type = string.Empty;
  47.                 var name = string.Empty;
  48.                 var damage = 45;
  49.                 var health = 250;
  50.                 var armor = 10;
  51.  
  52.                 var match = regex.Match(inputLine);
  53.                 type = match.Groups[1].Value;
  54.                 name = match.Groups[2].Value;
  55.                 if (match.Groups[3].Value != "null")
  56.                 {
  57.                     damage = int.Parse(match.Groups[3].Value);
  58.                 }
  59.  
  60.                 if (match.Groups[4].Value != "null")
  61.                 {
  62.                     health = int.Parse(match.Groups[4].Value);
  63.                 }
  64.  
  65.                 if (match.Groups[5].Value != "null")
  66.                 {
  67.                     armor = int.Parse(match.Groups[5].Value);
  68.                 }
  69.  
  70.                 if (!typesDragons.ContainsKey(type))
  71.                 {
  72.                     typesDragons.Add(type, new SortedDictionary<string, int[]>());
  73.                 }
  74.  
  75.                 if (!typesDragons[type].ContainsKey(name))
  76.                 {
  77.                     typesDragons[type].Add(name, new int[3]);
  78.                 }
  79.  
  80.                 typesDragons[type][name][0] = damage;
  81.                 typesDragons[type][name][1] = health;
  82.                 typesDragons[type][name][2] = armor;
  83.             }
  84.  
  85.             foreach (var type in typesDragons)
  86.             {
  87.                 var avrDamage = type.Value
  88.                     .Select(n => n.Value[0])
  89.                     .Average();
  90.  
  91.                 var avrHealth = type.Value
  92.                     .Select(n => n.Value[1])
  93.                     .Average();
  94.  
  95.                 var avrArmor = type.Value
  96.                     .Select(n => n.Value[2])
  97.                     .Average();
  98.  
  99.                 Console.WriteLine(
  100.                     "{0}::({1:F2}/{2:F2}/{3:F2})",
  101.                     type.Key,
  102.                     avrDamage,
  103.                     avrHealth,
  104.                     avrArmor);
  105.  
  106.                 foreach (var dragon in type.Value)
  107.                 {
  108.                     Console.WriteLine(
  109.                         "-{0} -> damage: {1}, health: {2}, armor: {3}",
  110.                         dragon.Key,
  111.                         dragon.Value[0],
  112.                         dragon.Value[1],
  113.                         dragon.Value[2]);
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement