Advertisement
Katina_

Race - Explained

Jul 25th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 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]+)"); //Pattern for finding a name
  14.             string patternDigits = @"(?<digits>\d+)"; //Pattern for finding digits
  15.  
  16.             int sumOfDigits = 0;
  17.  
  18.             //Dictionary for the participants
  19.             Dictionary<string, int> participants = new Dictionary<string, int>();
  20.  
  21.             //List of participants
  22.             List<string> names = Console.ReadLine()
  23.                 .Split(", ")
  24.                 .ToList();
  25.  
  26.             string input = Console.ReadLine();
  27.  
  28.             while (input != "end of race")
  29.             {
  30.                 var matchedNames = patternName.Matches(input); //Finding names(words)
  31.                 var matchedDigits = Regex.Matches(input, patternDigits); //Finding digits
  32.  
  33.                 string currentName = string.Join("", matchedNames); //Joining the words
  34.                 string currentDigit = string.Join("", matchedDigits); //Joining the digits
  35.  
  36.                 sumOfDigits = 0;
  37.  
  38.                 for (int i = 0; i < currentDigit.Length; i++)
  39.                 {
  40.                     //Converting the digits
  41.                     //Sum them
  42.                     sumOfDigits += int.Parse(currentDigit[i].ToString());
  43.                 }
  44.  
  45.                 //If our list of participants contains the current name
  46.                 if (names.Contains(currentName))
  47.                 {
  48.                     //If our participants dictionary does not contain the current name
  49.                     if (!participants.ContainsKey(currentName))
  50.                     {
  51.                         //We add the current name and the sum of the digits to the dictionary                  
  52.                         participants.Add(currentName, sumOfDigits);                      
  53.                     }
  54.  
  55.                     //else if our dictionary with participants contains the current name
  56.                     else
  57.                     {  
  58.                         //We add the sum to the old distance(sum) of the same participant
  59.                         participants[currentName] += sumOfDigits;                      
  60.                     }
  61.                 }
  62.  
  63.                 input = Console.ReadLine();
  64.             }
  65.  
  66.             var currentWinners = participants
  67.                .OrderByDescending(x => x.Value)
  68.                .Take(3);
  69.  
  70.             var firstPlace = currentWinners // We take the 1st player
  71.                 .Take(1);
  72.  
  73.             var secondPlace = currentWinners // We take the 2nd player
  74.                 .OrderByDescending(x => x.Value)
  75.                 .Take(2)
  76.                 .OrderBy(x=>x.Value)
  77.                 .Take(1);
  78.  
  79.             var thirdPlace=currentWinners  // We take the 3rd player
  80.                 .OrderBy(x=>x.Value)
  81.                 .Take(1);
  82.  
  83.             //Output
  84.             foreach (var firstName in firstPlace)
  85.             {
  86.                 Console.WriteLine($"1st place: {firstName.Key}");
  87.  
  88.             }
  89.  
  90.             foreach (var secondName in secondPlace)
  91.             {
  92.                 Console.WriteLine($"2nd place: {secondName.Key}");
  93.             }
  94.  
  95.             foreach (var thirdName in thirdPlace)
  96.             {
  97.                 Console.WriteLine($"3rd place: {thirdName.Key}");
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement