static void AddDwarfs(Dictionary <string, List <Dwarf> > dwarfs) { string input = null; while ((input = Console.ReadLine()) != "Once upon a time") { // name <:> color <:> phisics var inputArray = input.Split().ToArray().Where(x => x != "<:>").ToArray(); string name = inputArray[0]; string color = inputArray[1]; int phisics = int.Parse(inputArray[2]); if (!dwarfs.ContainsKey(color)) { dwarfs[color] = new List <Dwarf>(); } if (!dwarfs[color].Any(x => x.Name == name)) { var dwarf = new Dwarf(); dwarf.Name = name; dwarf.Color = color; dwarf.Phisics = phisics; dwarfs[color].Add(dwarf); Dwarfs.Add(dwarf); } else { var dwarf = dwarfs[color].FirstOrDefault(x => x.Name == name); dwarf.Phisics = Math.Max(dwarf.Phisics, phisics); } } }
static void Main(string[] args) { var colorsToDwarfs = new Dictionary <string, List <Dwarfs> >(); var allDwarfs = new List <Dwarfs>(); while (true) { var line = Console.ReadLine(); if (line == "Once upon a time") { break; } var dwarfInfo = line .Split(new[] { ' ', '<', ':', '>' }, StringSplitOptions.RemoveEmptyEntries) .ToArray(); string name = dwarfInfo[0]; string color = dwarfInfo[1]; int physics = int.Parse(dwarfInfo[2]); if (!colorsToDwarfs.ContainsKey(color)) { colorsToDwarfs[color] = new List <Dwarfs>(); } var dwByCurrColor = colorsToDwarfs[color]; var foundDwarf = dwByCurrColor .FirstOrDefault(d => d.Name == name); if (foundDwarf != null) { foundDwarf.Physics = Math.Max(physics, foundDwarf.Physics); } else { var dwarf = new Dwarfs { Name = name, Color = color, Physics = physics }; dwByCurrColor.Add(dwarf); allDwarfs.Add(dwarf); } } var result = allDwarfs .OrderByDescending(d => d.Physics) .ThenByDescending(d => colorsToDwarfs[d.Color].Count()) .ToList(); foreach (var dwarf in result) { Console.WriteLine($"({dwarf.Color}) {dwarf.Name} <-> {dwarf.Physics}"); } }
static void Main(string[] args) { // <collor, dwarf> var dwarfs = new Dictionary <string, List <Dwarf> >(); AddDwarfs(dwarfs); var sortedResult = Dwarfs .OrderByDescending(x => x.Phisics) .ThenByDescending(x => dwarfs[x.Color].Count) .ToList(); foreach (var dwarf in sortedResult) { Console.WriteLine($"({dwarf.Color}) {dwarf.Name} <-> {dwarf.Phisics}"); } }