Guest User

Untitled

a guest
Apr 15th, 2020
15
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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(" <:> ").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.  
  35. for (int i = 0; i < dwarfs.Count; i++)
  36. {
  37. if (dwarfs[i].Name == name)
  38. {
  39. isMatched = true;
  40. if (dwarfs[i].HatColor != hatColor)
  41. {
  42. dwarfs.Add(dwarf);
  43. }
  44. else if (dwarfs[i].Physics < physics)
  45. {
  46. dwarfs[i].Physics = physics;
  47. }
  48. break;
  49. }
  50. }
  51.  
  52. if (!isMatched)
  53. {
  54. dwarfs.Add(dwarf);
  55. }
  56. }
  57.  
  58. Dictionary<string, int> colors = new Dictionary<string, int>();
  59. for (int i = 0; i < dwarfs.Count; i++)
  60. {
  61. if (!colors.ContainsKey(dwarfs[i].HatColor))
  62. {
  63. colors.Add(dwarfs[i].HatColor, 0);
  64. }
  65.  
  66. colors[dwarfs[i].HatColor] += 1;
  67. }
  68. colors = colors.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
  69. List<Dwarf> sortedDwarfs = new List<Dwarf>();
  70. foreach (var color in colors.Keys)
  71. {
  72. for (int i = 0; i < dwarfs.Count; i++)
  73. {
  74. if (dwarfs[i].HatColor == color)
  75. {
  76. sortedDwarfs.Add(dwarfs[i]);
  77. }
  78. }
  79. }
  80. sortedDwarfs = sortedDwarfs.OrderByDescending(x => x.Physics).ToList();
  81. foreach (var item in sortedDwarfs)
  82. {
  83. Console.WriteLine($"({item.HatColor}) {item.Name} <-> {item.Physics}");
  84. }
  85.  
  86.  
  87. }
  88. }
  89. }
RAW Paste Data