Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2020
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _02.Race
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<string> participants = Console.ReadLine()
  13. .Split(",")
  14. .ToList();
  15.  
  16. Dictionary<string, int> racers = new Dictionary<string, int>();
  17.  
  18.  
  19. string patternName = @"(?<name>[A-Za-z]+)";
  20. string patternDistance = @"(?<distance>\d+)";
  21.  
  22. while (true)
  23. {
  24. string input = Console.ReadLine();
  25. string name = string.Empty;
  26. string distance = string.Empty;
  27.  
  28. if (input=="end of race")
  29. {
  30. break;
  31. }
  32.  
  33. Regex regexName = new Regex(patternName);
  34. Regex regexDistance = new Regex(patternDistance);
  35.  
  36. MatchCollection matchesName = regexName.Matches(input);
  37. MatchCollection matchesDistance = regexDistance.Matches(input);
  38.  
  39. foreach (Match item in matchesName)
  40. {
  41. name+= item.Groups[1].Value;
  42. }
  43.  
  44. foreach (Match item in matchesDistance)
  45. {
  46. distance += item.Groups[1].Value;
  47. }
  48.  
  49. int sumDistance = distance.Sum(x => int.Parse(x.ToString()));
  50.  
  51. if (!racers.ContainsKey(name))
  52. {
  53.  
  54. if (participants.Contains(name))
  55. {
  56. racers.Add(name, sumDistance);
  57. }
  58.  
  59.  
  60. }
  61.  
  62. else if (racers.ContainsKey(name))
  63. {
  64. if (participants.Contains(name))
  65. {
  66. racers[name] += sumDistance;
  67. }
  68. }
  69.  
  70.  
  71. }
  72.  
  73. int counter = 0;
  74. foreach (var item in racers.OrderByDescending(x=>x.Value))
  75. {
  76. counter++;
  77.  
  78. if (counter == 1)
  79. {
  80. Console.WriteLine($"1st place: {item.Key}");
  81. }
  82.  
  83. else if (counter == 2)
  84. {
  85. Console.WriteLine($"2nd place: {item.Key}");
  86. }
  87.  
  88. else if (counter==3)
  89. {
  90. Console.WriteLine($"3rd place: {item.Key}");
  91. break;
  92. }
  93.  
  94. }
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement