Advertisement
Guest User

Snowwhite

a guest
Apr 14th, 2020
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _4_SnowWhite
  6. {
  7.     class Dwarf
  8.     {
  9.         public Dwarf(string name, string hatColor, int physics)
  10.         {
  11.             Name = name;
  12.             HatColor = hatColor;
  13.             Physics = physics;
  14.         }
  15.         public string Name { get; set; }
  16.         public string HatColor { get; set; }
  17.         public int Physics { get; set; }
  18.  
  19.     }
  20.     class Program
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             string command = "";
  25.             List<Dwarf> dwarfs = new List<Dwarf>();
  26.             while ((command=Console.ReadLine())!="Once upon a time")
  27.             {
  28.                 string[] info = command.Split(" <:> ").Where(x => x != "<:>").ToArray(); ;
  29.                 string name = info[0];
  30.                 string hatColor = info[1];
  31.                 int physics = int.Parse(info[2]);
  32.                 Dwarf dwarf = new Dwarf(name, hatColor, physics);
  33.                 bool isMatched = false;
  34.                 for (int i = 0; i < dwarfs.Count; i++)
  35.                 {
  36.                     if (dwarfs[i].Name==name)
  37.                     {
  38.                         isMatched = true;
  39.                         if (dwarfs[i].HatColor!=hatColor)
  40.                         {
  41.                             dwarfs.Add(dwarf);
  42.                         }
  43.                         else
  44.                         {
  45.                             if (dwarfs[i].Physics<physics)
  46.                             {
  47.                                 dwarfs.RemoveAt(i);
  48.                                 dwarfs.Add(dwarf);
  49.                             }
  50.                         }
  51.  
  52.                     }
  53.                 }
  54.                 if (!isMatched)
  55.                 {
  56.                     dwarfs.Add(dwarf);
  57.                 }
  58.                
  59.             }
  60.  
  61.             Dictionary<string, int> colors = new Dictionary<string, int>();
  62.             for (int i = 0; i < dwarfs.Count; i++)
  63.             {
  64.                 if (!colors.ContainsKey(dwarfs[i].HatColor))
  65.                 {
  66.                     colors.Add(dwarfs[i].HatColor,0);
  67.                 }
  68.  
  69.                 colors[dwarfs[i].HatColor] += 1;
  70.             }
  71.             colors = colors.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  72.             List<Dwarf> sortedDwarfs = new List<Dwarf>();
  73.             foreach (var color in colors.Keys)
  74.             {
  75.                 for (int i = 0; i < dwarfs.Count; i++)
  76.                 {
  77.                     if (dwarfs[i].HatColor==color)
  78.                     {
  79.                         sortedDwarfs.Add(dwarfs[i]);
  80.                     }
  81.                 }
  82.             }
  83.             sortedDwarfs = sortedDwarfs.OrderByDescending(x => x.Physics).ToList();
  84.             foreach (var item in sortedDwarfs)
  85.             {
  86.                 Console.WriteLine($"({item.HatColor}) {item.Name} <-> {item.Physics}");
  87.             }
  88.            
  89.  
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement