Advertisement
Prohause

SnowWhite

Aug 10th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem04Exam05012018
  6. {
  7. internal class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. var dwarfs = new List<Dwarf>();
  12.  
  13. string input;
  14.  
  15. while (!(input = Console.ReadLine()).Equals("Once upon a time"))
  16. {
  17. var tokens = input.Split(new[] { " <:> " }, StringSplitOptions.RemoveEmptyEntries);
  18. var name = tokens[0];
  19. var color = tokens[1];
  20. var physics = int.Parse(tokens[2]);
  21.  
  22. if (!dwarfs.Any(p => p.Name == name && p.HatColor == color))
  23. {
  24. dwarfs.Add(new Dwarf() { Name = name, HatColor = color, Physics = physics });
  25. }
  26. else
  27. {
  28. if (dwarfs.First(p => p.Name == name && p.HatColor == color).Physics < physics)
  29. {
  30. dwarfs.First(p => p.Name == name && p.HatColor == color).Physics = physics;
  31. }
  32. }
  33. }
  34.  
  35. var hatCollection = new Dictionary<string, List<string>>();
  36.  
  37. foreach (var dwarf in dwarfs)
  38. {
  39. if (!hatCollection.ContainsKey(dwarf.HatColor))
  40. {
  41. hatCollection.Add(dwarf.HatColor, new List<string>());
  42. }
  43.  
  44. hatCollection[dwarf.HatColor].Add(dwarf.Name);
  45. }
  46.  
  47. foreach (var dwarf in dwarfs.OrderByDescending(p => p.Physics).ThenByDescending(p => hatCollection[p.HatColor].Count))
  48. {
  49. Console.WriteLine($"({dwarf.HatColor}) {dwarf.Name} <-> {dwarf.Physics}");
  50. }
  51. }
  52. }
  53.  
  54. internal class Dwarf
  55. {
  56. public string Name { get; set; }
  57. public string HatColor { get; set; }
  58. public int Physics { get; set; }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement