Advertisement
Katina_

Race

Jul 25th, 2019
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Race
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             var patternName = new Regex(@"(?<name>[A-Za-z]+)");
  14.             string patternDigits = @"(?<digits>\d+)";
  15.  
  16.             int sumOfDigits = 0;
  17.  
  18.             Dictionary<string, int> participants = new Dictionary<string, int>();
  19.  
  20.             List<string> names = Console.ReadLine()
  21.                 .Split(", ")
  22.                 .ToList();
  23.  
  24.             string input = Console.ReadLine();
  25.  
  26.             while (input != "end of race")
  27.             {
  28.                 var matchedNames = patternName.Matches(input);
  29.                 var matchedDigits = Regex.Matches(input, patternDigits);
  30.  
  31.                 string currentName = string.Join("", matchedNames);
  32.                 string currentDigit = string.Join("", matchedDigits);
  33.  
  34.                 sumOfDigits = 0;
  35.  
  36.                 for (int i = 0; i < currentDigit.Length; i++)
  37.                 {
  38.                     sumOfDigits += int.Parse(currentDigit[i].ToString());
  39.                 }
  40.  
  41.                 if (names.Contains(currentName))
  42.                 {
  43.  
  44.                     if (!participants.ContainsKey(currentName))
  45.                     {
  46.                         participants.Add(currentName, sumOfDigits);
  47.                     }
  48.  
  49.                     else
  50.                     {
  51.                         participants[currentName] += sumOfDigits;
  52.                     }
  53.                 }
  54.  
  55.                 input = Console.ReadLine();
  56.             }
  57.  
  58.             var currentWinners = participants
  59.                .OrderByDescending(x => x.Value)
  60.                .Take(3);
  61.  
  62.             var firstWinner = currentWinners
  63.                 .Take(1);
  64.  
  65.             var secondWinner = currentWinners
  66.                 .OrderByDescending(x => x.Value)
  67.                 .Take(2)
  68.                 .OrderBy(x => x.Value)
  69.                 .Take(1);
  70.  
  71.             var thirdWinner = currentWinners
  72.                 .OrderBy(x => x.Value)
  73.                 .Take(1);
  74.  
  75.             foreach (var firstName in firstWinner)
  76.             {
  77.                 Console.WriteLine($"1st place: {firstName.Key}");
  78.  
  79.             }
  80.  
  81.             foreach (var secondName in secondWinner)
  82.             {
  83.                 Console.WriteLine($"2nd place: {secondName.Key}");
  84.             }
  85.  
  86.             foreach (var thirdName in thirdWinner)
  87.             {
  88.                 Console.WriteLine($"3rd place: {thirdName.Key}");
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement