Advertisement
yanass

Race - Regular Expressions

Jul 30th, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Race
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             string[] names = Console.ReadLine()
  13.                 .Split(", ")
  14.                 .ToArray();
  15.  
  16.             Dictionary<string, int> racers = new Dictionary<string, int>();
  17.  
  18.             for (int i = 0; i < names.Length; i++)
  19.             {
  20.                 racers.Add(names[i], 0);
  21.             }
  22.  
  23.             string racerInfo = Console.ReadLine();
  24.  
  25.             while (racerInfo != "end of race")
  26.             {
  27.                 string pattern = @"(?<name>[A-Za-z]+)|(?<distance>\[0-9])";
  28.  
  29.  
  30.                 MatchCollection nameDistance = Regex.Matches(racerInfo, pattern);
  31.  
  32.                 string name = "";
  33.  
  34.  
  35.                 foreach (Match item in nameDistance)
  36.                 {
  37.                     name += item.Groups["name"].Value;
  38.                 }
  39.  
  40.                 int splitDigits = 0;
  41.  
  42.                 foreach (Match item in nameDistance)
  43.                 {
  44.                     splitDigits += int.Parse(item.Groups["distance"].Value);
  45.                 }
  46.  
  47.                 if (racers.ContainsKey(name))
  48.                 {
  49.                     racers[name] += splitDigits;
  50.                 }
  51.                 racerInfo = Console.ReadLine();
  52.             }
  53.  
  54.             foreach (var racer in racers)
  55.             {
  56.                 Console.WriteLine($"{racer.Key}: {racer.Value}");
  57.             }
  58.  
  59.  
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement