Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace examprep2retake4
  6. {
  7.     class Dwarf
  8.     {
  9.         public string Name { get; set; }
  10.        
  11.         public string Color { get; set; }
  12.  
  13.         public int Physics { get; set; }
  14.     }
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             var colorsToDwarfs = new Dictionary<string, List<Dwarf>>();
  20.             var allDwarfs = new List<Dwarf>();
  21.             while (true)
  22.             {
  23.                 string line = Console.ReadLine();
  24.  
  25.                 if (line == "Once upon a time")
  26.                 {
  27.                     break;
  28.                 }
  29.  
  30.                 string[] dwarfInfo = line.Split(
  31.                     new char[] {' ', '<', ':', '>'},
  32.                     StringSplitOptions.RemoveEmptyEntries);
  33.                 string name = dwarfInfo[0];
  34.                 string color = dwarfInfo[1];
  35.                 int physics = int.Parse(dwarfInfo[2]);
  36.  
  37.                 if (!colorsToDwarfs.ContainsKey(color))
  38.                 {
  39.                     colorsToDwarfs[color] = new List<Dwarf>();
  40.                 }
  41.  
  42.                 var dwarfsByCurrentColor = colorsToDwarfs[color];
  43.                 var foundDwarf = dwarfsByCurrentColor
  44.                     .FirstOrDefault(d => d.Name == name);
  45.  
  46.                 if (foundDwarf != null)
  47.                 {
  48.                     foundDwarf.Physics = Math.Max(physics, foundDwarf.Physics);
  49.                 }
  50.                 else
  51.                 {
  52.                     var dwarf = new Dwarf
  53.                     {
  54.                         Name = name,
  55.                         Color = color,
  56.                         Physics = physics
  57.                     };
  58.  
  59.                     dwarfsByCurrentColor.Add(dwarf);
  60.                     allDwarfs.Add(dwarf);
  61.                 }
  62.             }
  63.             var result = allDwarfs
  64.                 .OrderByDescending(d => d.Physics)
  65.                 .ThenByDescending(d => colorsToDwarfs[d.Color].Count())
  66.                 .ToList();
  67.  
  68.             foreach (var dwarf in result)
  69.             {
  70.                 Console.WriteLine($"({dwarf.Color}) {dwarf.Name} <-> {dwarf.Physics}");
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement