TheBulgarianWolf

Racers

Apr 15th, 2021
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Race
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, int> racers = Console.ReadLine().Split(", ", StringSplitOptions.RemoveEmptyEntries).ToDictionary(x => x, x => 0);
  14.  
  15.             Regex nameRegex = new Regex(@"[A-Za-z]+");
  16.             Regex digitRegex = new Regex(@"\d");
  17.  
  18.             while (true)
  19.             {
  20.                 string line = Console.ReadLine();
  21.                 if(line == "end of race")
  22.                 {
  23.                     break;
  24.                 }
  25.  
  26.                 MatchCollection letterMatches = nameRegex.Matches(line);
  27.                 MatchCollection digitMatches = digitRegex.Matches(line);
  28.                 string name = GetName(letterMatches);
  29.                 int sum = GetSum(digitMatches);
  30.                 if (!racers.ContainsKey(name))
  31.                 {
  32.                     continue;
  33.                 }
  34.  
  35.                 racers[name] += sum;
  36.             }
  37.  
  38.             string[] winners = racers.OrderByDescending(r => r.Value).Take(3).Select(r => r.Key).ToArray();
  39.             Console.WriteLine("1st place: " + winners[0]);
  40.             Console.WriteLine("2nd place: " + winners[1]);
  41.             Console.WriteLine("3rd place: " + winners[2]);
  42.         }
  43.  
  44.         private static int GetSum(MatchCollection digitMatches)
  45.         {
  46.             int sum = 0;
  47.             foreach(Match match in digitMatches)
  48.             {
  49.                 sum += int.Parse(match.Value);
  50.             }
  51.             return sum;
  52.         }
  53.  
  54.         private static string GetName(MatchCollection matches)
  55.         {
  56.             StringBuilder sb = new StringBuilder();
  57.             foreach (Match match in matches)
  58.             {
  59.                 sb.Append(match.Value);
  60.             }
  61.             return sb.ToString();
  62.         }
  63.     }
  64. }
  65.  
Add Comment
Please, Sign In to add comment