WindFell

SnowWhite

Jun 29th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Snowwhite
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         List<Dwarf> dwarfs = new List<Dwarf>();
  10.         var hats = new Dictionary<string, int>();
  11.  
  12.         string input;
  13.  
  14.         while ((input = Console.ReadLine()) != "Once upon a time")
  15.         {
  16.             string[] dwarfStats = input
  17.                 .Split(" <:>".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  18.                 .ToArray();
  19.  
  20.             string dwarfName = dwarfStats[0];
  21.             string dwarfHatColor = dwarfStats[1];
  22.             int dwarfPhysics = int.Parse(dwarfStats[2]);
  23.  
  24.             Dwarf dwarf = new Dwarf(dwarfName, dwarfHatColor, dwarfPhysics);
  25.             Dwarf sameDwarf = dwarfs.FirstOrDefault(d => d.Name == dwarf.Name
  26.                 && d.HatColor == dwarf.HatColor);
  27.  
  28.             if (sameDwarf != null)
  29.             {
  30.                 if (sameDwarf.Physics < dwarf.Physics)
  31.                 {
  32.                     sameDwarf.Physics = dwarf.Physics;
  33.                 }
  34.             }
  35.             else
  36.             {
  37.                 dwarfs.Add(dwarf);
  38.  
  39.                 if (hats.ContainsKey(dwarf.HatColor) == false)
  40.                 {
  41.                     hats.Add(dwarf.HatColor, 1);
  42.                 }
  43.  
  44.                 hats[dwarf.HatColor]++;
  45.             }
  46.         }
  47.  
  48.         foreach (Dwarf dwarf in dwarfs.OrderByDescending(d => d.Physics).ThenByDescending(d => hats[d.HatColor]))
  49.         {
  50.             Console.WriteLine(dwarf);
  51.         }
  52.     }
  53. }
  54.  
  55. class Dwarf
  56. {
  57.     public Dwarf(string name, string hatColor, int physics)
  58.     {
  59.         this.Name = name;
  60.         this.HatColor = hatColor;
  61.         this.Physics = physics;
  62.     }
  63.  
  64.     public string Name { get; }
  65.     public string HatColor { get; }
  66.     public int Physics { get; set; }
  67.  
  68.     public override string ToString()
  69.     {
  70.         string result = $"({this.HatColor}) {this.Name} <-> {this.Physics}";
  71.         return result;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment