Boris-Stavrev92

Untitled

Apr 11th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03_04_SnowWhite
  6. {
  7.     class SnowWhite
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             HashSet<Hat> hats = new HashSet<Hat>();
  12.             List<Dwarf> dwarfs = new List<Dwarf>();
  13.             string input = string.Empty;
  14.  
  15.             while ((input = Console.ReadLine()) != "Once upon a time")
  16.             {
  17.                 string[] data = input.Split(new char[] { ' ', '<', ':', '>' }, StringSplitOptions.RemoveEmptyEntries);
  18.                 string name = data[0];
  19.                 string hat = data[1];
  20.                 long physics = long.Parse(data[2]);
  21.  
  22.  
  23.                 if (dwarfs.Select(x => x.Name).Contains(name) && dwarfs.Where(x => x.Name == name).Select(x => x.Hat.Name).Contains(hat))
  24.                 {
  25.                     Dwarf repeatedDwarf = dwarfs.Single(x => x.Name == name && x.Hat.Name == hat);
  26.                     repeatedDwarf.Physics = repeatedDwarf.Physics < physics ? physics : repeatedDwarf.Physics;
  27.                 }
  28.                 else
  29.                 {
  30.                     if (hats.Select(x => x.Name).Contains(hat))
  31.                     {
  32.                         hats.Single(x => x.Name == hat).Count++;
  33.                     }
  34.                     else
  35.                     {
  36.                         hats.Add(new Hat(hat));
  37.                     }
  38.                     dwarfs.Add(new Dwarf(name, hats.Single(x => x.Name == hat), physics));
  39.                 }
  40.             }
  41.  
  42.             foreach (Dwarf dwarf in dwarfs.OrderByDescending(x => x.Physics).ThenByDescending(x => x.Hat.Count))
  43.             {
  44.                 Console.WriteLine($"({dwarf.Hat.Name}) {dwarf.Name} <-> {dwarf.Physics}");
  45.             }
  46.         }
  47.     }
  48.  
  49.     class Dwarf
  50.     {
  51.         public Hat Hat { get; private set; }
  52.         public string Name { get; private set; }
  53.         public long Physics { get; set; }
  54.  
  55.         public Dwarf(string name, Hat hat, long physics)
  56.         {
  57.             this.Hat = hat;
  58.             this.Name = name;
  59.             this.Physics = physics;
  60.         }
  61.     }
  62.  
  63.     class Hat
  64.     {
  65.         public string Name { get; private set; }
  66.         public long Count { get; set; }
  67.  
  68.         public Hat(string name)
  69.         {
  70.             this.Name = name;
  71.             this.Count = 0;
  72.         }
  73.     }
  74. }
Add Comment
Please, Sign In to add comment