Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DragonArmy
  6. {
  7. class Program
  8. {
  9. private static Dictionary<string, SortedDictionary<string, Dictionary<string, int>>> data = new Dictionary<string, SortedDictionary<string, Dictionary<string, int>>>();
  10.  
  11. private static Dictionary<string, int> defaultValues = new Dictionary<string, int>
  12. {
  13. { "damage", 45},
  14. { "health", 250},
  15. { "armor", 10}
  16. };
  17. private static int n = 0;
  18.  
  19. private static void CollectDragonData(string[] input)
  20. {
  21. string type = input[0],
  22. name = input[1];
  23. int tempInt;
  24.  
  25. if (!data.ContainsKey(type))
  26. {
  27. data[type] = new SortedDictionary<string, Dictionary<string, int>>();
  28. }
  29. if (!data[type].ContainsKey(name))
  30. {
  31. data[type][name] = defaultValues.ToDictionary(x => x.Key, x => x.Value);
  32. }
  33. /* try to parse the damage */
  34. if(int.TryParse(input[2], out tempInt)){
  35. data[type][name]["damage"] = int.Parse(input[2]);
  36. }
  37. /* try to parse the health */
  38. if (int.TryParse(input[3], out tempInt))
  39. {
  40. data[type][name]["health"] = int.Parse(input[3]);
  41. }
  42. /* try to parse the armor */
  43. if (int.TryParse(input[4], out tempInt))
  44. {
  45. data[type][name]["armor"] = int.Parse(input[4]);
  46. }
  47. }
  48.  
  49. private static void PrintData()
  50. {
  51. foreach (var type in data)
  52. {
  53. PrintTypeData(type);
  54. foreach (var dragon in type.Value)
  55. {
  56. Console.WriteLine(
  57. $"-{dragon.Key} -> damage: {dragon.Value["damage"]}, health: {dragon.Value["health"]}, armor: {dragon.Value["armor"]}"
  58. );
  59. }
  60. }
  61. }
  62.  
  63. private static void PrintTypeData(KeyValuePair<string, SortedDictionary<string, Dictionary<string, int>>> type)
  64. {
  65. var damages = type.Value
  66. .SelectMany(x => x.Value.Where(y => y.Key == "damage"))
  67. .ToArray();
  68. double avgDamage = damages.Average(x => x.Value);
  69. var armors = type.Value
  70. .SelectMany(x => x.Value.Where(y => y.Key == "armor"))
  71. .ToArray();
  72. double avgArmor = armors.Average(x => x.Value);
  73. var healths = type.Value
  74. .SelectMany(x => x.Value.Where(y => y.Key == "health"))
  75. .ToArray();
  76. double avgHealth = healths.Average(x => x.Value);
  77. Console.WriteLine($"{type.Key}::({avgDamage:f2}/{avgHealth:f2}/{avgArmor:f2})");
  78. }
  79.  
  80. private static void Main(string[] args)
  81. {
  82. n = int.Parse(Console.ReadLine());
  83. var input = new string[5];
  84. for (int i = 0; i < n; i++)
  85. {
  86. input = Console.ReadLine()
  87. .Split(' ')
  88. .ToArray();
  89. CollectDragonData(input);
  90. }
  91. PrintData();
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement