Advertisement
Rayk

Snowwhite

Feb 28th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 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.         Dictionary<string, int> dwarfs = new Dictionary<string, int>();
  10.         Dictionary<string, int> colors = new Dictionary<string, int>();
  11.  
  12.         string line;
  13.         while ((line = Console.ReadLine()) != "Once upon a time")
  14.         {
  15.             string[] tokens = line
  16.                 .Split(new[] { " <:> " }, StringSplitOptions.RemoveEmptyEntries);
  17.  
  18.             string dwarfName = tokens[0];
  19.             string dwarfHatColor = tokens[1];
  20.             int dwarfPhysics = int.Parse(tokens[2]);
  21.  
  22.             string currentDwarfId = $"{dwarfName} <:> {dwarfHatColor}";
  23.  
  24.             if (dwarfs.ContainsKey(currentDwarfId) == false)
  25.             {
  26.                 dwarfs.Add(currentDwarfId, dwarfPhysics);
  27.  
  28.                 if (colors.ContainsKey(dwarfHatColor) == false)
  29.                 {
  30.                     colors.Add(dwarfHatColor, 1);
  31.                 }
  32.                 else
  33.                 {
  34.                     colors[dwarfHatColor]++;
  35.                 }
  36.             }
  37.             else
  38.             {
  39.                 int oldValue = dwarfs[currentDwarfId];
  40.                 if (dwarfPhysics > oldValue)
  41.                 {
  42.                     dwarfs[currentDwarfId] = dwarfPhysics;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         Dictionary<string, int> sortedDwarfs = dwarfs
  48.             .OrderByDescending(d => d.Value)
  49.             .ThenByDescending(d => colors[d.Key.Split(new[] { " <:> " }, StringSplitOptions.RemoveEmptyEntries)[1]])
  50.             .ToDictionary(x => x.Key, x => x.Value);
  51.  
  52.         foreach (var dwarf in sortedDwarfs)
  53.         {
  54.             string dwarfId = dwarf.Key;
  55.             string[] dwarfIdTokens = dwarfId.Split(new[] { " <:> " }, StringSplitOptions.RemoveEmptyEntries);
  56.  
  57.             string dwarfName = dwarfIdTokens[0];
  58.             string dwarfHatColor = dwarfIdTokens[1];
  59.  
  60.             int dwarfPhsyics = dwarf.Value;
  61.  
  62.             Console.WriteLine($"({dwarfHatColor}) {dwarfName} <-> {dwarfPhsyics}");
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement