private static void AddingNewDragon(Dictionary <string, TypeDragonAverageStats> listOfDragonsAverage, Dictionary <string, List <NamedDragonStats> > namedDragonsList, string type, string name, NamedDragonStats newDragon) { if (!namedDragonsList.ContainsKey(type)) { namedDragonsList.Add(type, new List <NamedDragonStats>()); namedDragonsList[type].Add(newDragon); TypeDragonAverageStats newAverageDragon = new TypeDragonAverageStats(); newAverageDragon.Armour = newDragon.Armour; newAverageDragon.Damage = newDragon.Damage; newAverageDragon.Health = newDragon.Health; newAverageDragon.Count++; listOfDragonsAverage[type] = newAverageDragon; } else { if (namedDragonsList[type].Any(x => x.Name == name)) { NamedDragonStats statsToSubstract = namedDragonsList[type].Find(x => x.Name == name); listOfDragonsAverage[type].Armour -= statsToSubstract.Armour; listOfDragonsAverage[type].Damage -= statsToSubstract.Damage; listOfDragonsAverage[type].Health -= statsToSubstract.Health; statsToSubstract.Armour = newDragon.Armour; statsToSubstract.Damage = newDragon.Damage; statsToSubstract.Health = newDragon.Health; listOfDragonsAverage[type].Armour += statsToSubstract.Armour; listOfDragonsAverage[type].Damage += statsToSubstract.Damage; listOfDragonsAverage[type].Health += statsToSubstract.Health; } else { namedDragonsList[type].Add(newDragon); listOfDragonsAverage[type].Armour += newDragon.Armour; listOfDragonsAverage[type].Damage += newDragon.Damage; listOfDragonsAverage[type].Health += newDragon.Health; listOfDragonsAverage[type].Count++; } } }
static void Main(string[] args) { var listOfDragonsAverage = new Dictionary <string, TypeDragonAverageStats>(); var namedDragonsList = new Dictionary <string, List <NamedDragonStats> >(); int countDragons = int.Parse(Console.ReadLine()); for (int i = 0; i < countDragons; i++) { string[] inputArr = Console.ReadLine().Split().ToArray(); string type = inputArr[0]; string name = inputArr[1]; NamedDragonStats newDragon = new NamedDragonStats(name); if (inputArr[2] != "null") { double damage = double.Parse(inputArr[2]); newDragon.Damage = damage; } if (inputArr[3] != "null") { double health = double.Parse(inputArr[3]); newDragon.Health = health; } if (inputArr[4] != "null") { double armour = double.Parse(inputArr[4]); newDragon.Armour = armour; } AddingNewDragon(listOfDragonsAverage, namedDragonsList, type, name, newDragon); } Printing(listOfDragonsAverage, namedDragonsList); }