Advertisement
joro_thexfiles

Race

Jul 28th, 2019
1,486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5.  
  6. namespace _2._Race
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var dict = new Dictionary<string, double>();
  13.  
  14.             string[] players = Console.ReadLine()
  15.                 .Split(", ");
  16.  
  17.             foreach (string player in players)
  18.             {
  19.                 if (!dict.ContainsKey(player))
  20.                 {
  21.                     dict[player] = 0;
  22.                 }
  23.             }
  24.  
  25.             while (true)
  26.             {
  27.                 string text = Console.ReadLine();
  28.                 string name = string.Empty;
  29.                 double distance = 0;
  30.  
  31.                 if (text == "end of race")
  32.                 {
  33.                     break;
  34.                 }
  35.                 else
  36.                 {
  37.                     Regex regex = new Regex(@"[A-Za-z]");
  38.  
  39.                     MatchCollection matches = regex.Matches(text);
  40.  
  41.                     foreach (Match match in matches)
  42.                     {
  43.                         name += match.Value;
  44.                     }
  45.  
  46.                     if (dict.ContainsKey(name))
  47.                     {
  48.                         MatchCollection matchesForDistance = Regex.Matches(text, @"[0-9]");
  49.  
  50.                         foreach (Match item in matchesForDistance)
  51.                         {
  52.                             distance += double.Parse(item.Value);
  53.                         }
  54.  
  55.                         dict[name] += distance;
  56.                     }
  57.                 }        
  58.             }
  59.             dict = dict
  60.                 .OrderByDescending(x => x.Value)
  61.                 .ToDictionary(x => x.Key, y => y.Value);
  62.  
  63.             int counter = 1;
  64.  
  65.             foreach (var kvp in dict)
  66.             {
  67.                 if (counter ==1)
  68.                 {
  69.                     Console.WriteLine($"1st place: {kvp.Key}");
  70.                 }
  71.                 else if (counter ==2)
  72.                 {
  73.                     Console.WriteLine($"2nd place: {kvp.Key}");
  74.                 }
  75.                 else if (counter == 3)
  76.                 {
  77.                     Console.WriteLine($"3rd place: {kvp.Key}");
  78.                 }
  79.                 else
  80.                 {
  81.                     break;
  82.                 }
  83.                 counter++;
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement