Advertisement
amphibia89

Dragon Army

May 28th, 2016
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. class Program
  7. {
  8.     public static void Main()
  9.     {
  10. #if DEBUG
  11.         Console.SetIn(new StreamReader("../../input.txt"));
  12. #endif
  13.         var dragonDictionary = new Dictionary<string, SortedSet<Dragon>>();
  14.  
  15.         int dragonCount = int.Parse(Console.ReadLine());
  16.         for (int counter = 0; counter < dragonCount; counter++)
  17.         {
  18.             Dragon dragon = ReadDragon();
  19.             UpdateDragonDictionary(dragonDictionary, dragon);
  20.         }
  21.  
  22.         foreach (var pair in dragonDictionary)
  23.         {
  24.             Console.WriteLine("{0}::({1:F2}/{2:F2}/{3:F2})",
  25.                 pair.Key,
  26.                 pair.Value
  27.                     .Select(dragon => dragon.Damage)
  28.                     .Average(),
  29.                 pair.Value
  30.                     .Select(dragon => dragon.Health)
  31.                     .Average(),
  32.                 pair.Value
  33.                     .Select(dragon => dragon.Armor)
  34.                     .Average());
  35.  
  36.             Console.WriteLine(string.Join("\n", pair.Value));
  37.         }
  38.     }
  39.  
  40.     private static void UpdateDragonDictionary(Dictionary<string, SortedSet<Dragon>> dragonDictionary, Dragon dragon)
  41.     {
  42.         if (!dragonDictionary.ContainsKey(dragon.Type))
  43.         {
  44.             dragonDictionary[dragon.Type] =
  45.                 new SortedSet<Dragon>(DragonComparer.GetComparer());
  46.         }
  47.  
  48.         if (dragonDictionary[dragon.Type].Contains(dragon))
  49.             dragonDictionary[dragon.Type].Remove(dragon);
  50.  
  51.         dragonDictionary[dragon.Type].Add(dragon);
  52.     }
  53.  
  54.     private static Dragon ReadDragon()
  55.     {
  56.         string[] dragonArgs = Console.ReadLine().Split();
  57.  
  58.         string type = dragonArgs[0];
  59.         string name = dragonArgs[1];
  60.         int? damage = (dragonArgs[2] != "null")
  61.             ? (int?)int.Parse(dragonArgs[2])
  62.             : null;
  63.         int? health = (dragonArgs[3] != "null")
  64.             ? (int?)int.Parse(dragonArgs[3])
  65.             : null;
  66.         int? armor = (dragonArgs[4] != "null")
  67.             ? (int?)int.Parse(dragonArgs[4])
  68.             : null;
  69.  
  70.         return new Dragon(type, name, damage, health, armor);
  71.     }
  72. }
  73.  
  74. class Dragon
  75. {
  76.     private const int DefaultDamage = 45;
  77.     private const int DefaultHealth = 250;
  78.     private const int DefaultArmor = 10;
  79.  
  80.     public string Name { get; private set; }
  81.     public string Type { get; private set; }
  82.     public int Damage { get; private set; }
  83.     public int Health { get; private set; }
  84.     public int Armor { get; private set; }
  85.  
  86.     public Dragon(string type, string name, int? damage, int? health, int? armor)
  87.     {
  88.         Name = name;
  89.         Type = type;
  90.         Damage = damage ?? DefaultDamage;
  91.         Health = health ?? DefaultHealth;
  92.         Armor = armor ?? DefaultArmor;
  93.     }
  94.  
  95.     // override object.Equals
  96.     public override bool Equals(object obj)
  97.     {
  98.         if (obj is Dragon && obj != null)
  99.         {
  100.             Dragon dragon = (Dragon)obj;
  101.             return (dragon.Name == this.Name && dragon.Type == this.Type);
  102.         }
  103.  
  104.         return false;
  105.     }
  106.  
  107.     // override object.GetHashCode
  108.     public override int GetHashCode()
  109.     {
  110.         return (Name + Type).GetHashCode();
  111.     }
  112.  
  113.     // override object.ToString
  114.     public override string ToString()
  115.     {
  116.         return string.Format("-{0} -> damage: {1}, health: {2}, armor: {3}",
  117.             Name,
  118.             Damage,
  119.             Health,
  120.             Armor);
  121.     }
  122. }
  123.  
  124. class DragonComparer : IComparer<Dragon>
  125. {
  126.     public int Compare(Dragon x, Dragon y)
  127.     {
  128.         return string.Compare(x.Name, y.Name);
  129.     }
  130.  
  131.     public static IComparer<Dragon> GetComparer()
  132.     {
  133.         return new DragonComparer();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement