Advertisement
miroLLL

11Problem-DragonArmy

Feb 16th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _11Problem_DragonArmy
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // Read the input
  12.             int numberOfDragons = int.Parse(Console.ReadLine());
  13.  
  14.             // Keep: Key = Dragon Type , Value = (Key = Dragon Name) + (Value = List with damage, health and armor).
  15.             var dragonsTypeAndName = new Dictionary<string, SortedDictionary<string, List<double>>>();
  16.  
  17.             for (int i = 1; i <= numberOfDragons; i++)
  18.             {
  19.                 string[] inputDragonInfo = Console.ReadLine().Split();
  20.  
  21.                 string type = inputDragonInfo[0];
  22.                 string name = inputDragonInfo[1];
  23.  
  24.                 uint damage;
  25.                 uint health;
  26.                 uint armor;
  27.  
  28.                 bool tryDamage = uint.TryParse(inputDragonInfo[2], out damage);
  29.                 bool tryHealth = uint.TryParse(inputDragonInfo[3], out health);
  30.                 bool tryArmor = uint.TryParse(inputDragonInfo[4], out armor);
  31.  
  32.                 if (tryDamage == false)
  33.                 {
  34.                     damage = 45;
  35.                 }
  36.  
  37.                 if (tryHealth == false)
  38.                 {
  39.                     health = 250;
  40.                 }
  41.  
  42.                 if (tryArmor == false)
  43.                 {
  44.                     armor = 10;
  45.                 }
  46.  
  47.                 if (dragonsTypeAndName.ContainsKey(type) == false)
  48.                 {
  49.                     // Fill the dictionary with new type of dragon, which have a name and list of damage, health and armor.
  50.                     dragonsTypeAndName.Add(type, new SortedDictionary<string, List<double>>());
  51.                     dragonsTypeAndName[type].Add(name, new List<double>());
  52.                     dragonsTypeAndName[type][name].Add(damage);
  53.                     dragonsTypeAndName[type][name].Add(health);
  54.                     dragonsTypeAndName[type][name].Add(armor);
  55.                 }
  56.                 else
  57.                 {
  58.                     // Fill the dictionary with the existing type with new name and list of damage, health and armor.
  59.                     if (dragonsTypeAndName[type].ContainsKey(name) == false)
  60.                     {
  61.                         dragonsTypeAndName[type].Add(name, new List<double>());
  62.                         dragonsTypeAndName[type][name].Add(damage);
  63.                         dragonsTypeAndName[type][name].Add(health);
  64.                         dragonsTypeAndName[type][name].Add(armor);
  65.                     }
  66.                     else
  67.                     {
  68.                         // Overwrite the dragons stats (damage, health and armor) on already existing type and name of dragon.
  69.                         dragonsTypeAndName[type][name][0] = damage;
  70.                         dragonsTypeAndName[type][name][1] = health;
  71.                         dragonsTypeAndName[type][name][2] = armor;
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             foreach (var typeName in dragonsTypeAndName)
  77.             {
  78.                 double avgDmg = typeName.Value.Select(x => x.Value[0]).Average();
  79.                 double avgHeal = typeName.Value.Select(x => x.Value[1]).Average();
  80.                 double avgArm = typeName.Value.Select(x => x.Value[2]).Average();
  81.  
  82.                 Console.WriteLine($"{typeName.Key}::({avgDmg:f2}/{avgHeal:f2}/{avgArm:f2})");
  83.  
  84.                 foreach (var nameStats in typeName.Value)
  85.                 {
  86.                     Console.WriteLine($"-{nameStats.Key} -> damage: {nameStats.Value[0]}, health: {nameStats.Value[1]}, armor: {nameStats.Value[2]}");
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement