simonradev

Football League

Dec 10th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.35 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. using System.Threading.Tasks;
  7.  
  8. namespace FootballLeague
  9. {
  10.     class Program
  11.     {
  12.         public static Regex teams;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string enteredPattern = Console.ReadLine();
  17.  
  18.             StringBuilder pattern = EscapeTheCharsInThePatt(enteredPattern);
  19.  
  20.             try
  21.             {
  22.                 teams = new Regex(enteredPattern + @"(\w+)" + enteredPattern);
  23.             }
  24.             catch (Exception)
  25.             {
  26.                 teams = new Regex(pattern.ToString() + @"(\w+)" + pattern.ToString());
  27.             }
  28.  
  29.             Regex goals = new Regex(@"(\d+):(\d+)");
  30.  
  31.             Dictionary<string, long> teamsResults = new Dictionary<string, long>();
  32.             Dictionary<string, long> top3ScoredGoals = new Dictionary<string, long>();
  33.  
  34.             string inputLine = Console.ReadLine();
  35.             while (!inputLine.Equals("final"))
  36.             {
  37.                 MatchCollection allTeams = teams.Matches(inputLine);
  38.  
  39.                 string[] twoTeamsCompeting = new string[2];
  40.                 int index = 0;
  41.                 foreach (Match match in allTeams)
  42.                 {
  43.                     twoTeamsCompeting[index] = string.Join("", match.Groups[1].ToString().ToUpper().Reverse());
  44.  
  45.                     index++;
  46.                 }
  47.  
  48.                 Match results = goals.Match(inputLine);
  49.  
  50.                 long[] teamsGoals = new long[2];
  51.                 teamsGoals[0] = long.Parse(results.Groups[1].ToString());
  52.                 teamsGoals[1] = long.Parse(results.Groups[2].ToString());
  53.  
  54.  
  55.                 for (int currTeam = 0; currTeam < twoTeamsCompeting.Length; currTeam++)
  56.                 {
  57.                     if (!top3ScoredGoals.ContainsKey(twoTeamsCompeting[currTeam]))
  58.                     {
  59.                         top3ScoredGoals[twoTeamsCompeting[currTeam]] = teamsGoals[currTeam];
  60.                     }
  61.                     else
  62.                     {
  63.                         top3ScoredGoals[twoTeamsCompeting[currTeam]] += teamsGoals[currTeam];
  64.                     }
  65.                 }
  66.  
  67.  
  68.                 //if first team wins
  69.                 if (teamsGoals[0] > teamsGoals[1])
  70.                 {
  71.                     //appending points to the winner
  72.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
  73.                     {
  74.                         teamsResults.Add(twoTeamsCompeting[0], 3);
  75.                     }
  76.                     else
  77.                     {
  78.                         teamsResults[twoTeamsCompeting[0]] += 3;
  79.                     }
  80.  
  81.                     //appending points to the loser
  82.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
  83.                     {
  84.                         teamsResults.Add(twoTeamsCompeting[1], 0);
  85.                     }
  86.                     else
  87.                     {
  88.                         teamsResults[twoTeamsCompeting[1]] += 0;
  89.                     }
  90.                 }
  91.                 //if the second team wins
  92.                 else if (teamsGoals[0] < teamsGoals[1])
  93.                 {
  94.                     //appending points to the winner
  95.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
  96.                     {
  97.                         teamsResults.Add(twoTeamsCompeting[1], 3);
  98.                     }
  99.                     else
  100.                     {
  101.                         teamsResults[twoTeamsCompeting[1]] += 3;
  102.                     }
  103.  
  104.                     //appending points to the loser
  105.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
  106.                     {
  107.                         teamsResults.Add(twoTeamsCompeting[0], 0);
  108.                     }
  109.                     else
  110.                     {
  111.                         teamsResults[twoTeamsCompeting[0]] += 0;
  112.                     }
  113.                 }
  114.                 //if the match is draw
  115.                 else
  116.                 {
  117.                     //appending points to the winner
  118.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[0]))
  119.                     {
  120.                         teamsResults.Add(twoTeamsCompeting[0], 1);
  121.                     }
  122.                     else
  123.                     {
  124.                         teamsResults[twoTeamsCompeting[0]] += 1;
  125.                     }
  126.  
  127.                     //appending points to the loser
  128.                     if (!teamsResults.ContainsKey(twoTeamsCompeting[1]))
  129.                     {
  130.                         teamsResults.Add(twoTeamsCompeting[1], 1);
  131.                     }
  132.                     else
  133.                     {
  134.                         teamsResults[twoTeamsCompeting[1]] += 1;
  135.                     }
  136.                 }
  137.  
  138.                 inputLine = Console.ReadLine();
  139.             }
  140.  
  141.             //print LeagueStandings
  142.             Console.WriteLine("League standings:");
  143.             int counterForPlaceInTable = 1;
  144.             foreach (KeyValuePair<string, long> teamPointsPair in teamsResults
  145.                                                                   .OrderByDescending(p => p.Value)
  146.                                                                   .ThenBy(n => n.Key))
  147.             {
  148.                 Console.WriteLine($"{counterForPlaceInTable}. {teamPointsPair.Key} {teamPointsPair.Value}");
  149.  
  150.                 counterForPlaceInTable++;
  151.             }
  152.  
  153.             //print top 3 goals
  154.             Console.WriteLine("Top 3 scored goals:");
  155.             foreach (KeyValuePair<string, long> topGoalsPair in top3ScoredGoals
  156.                                                                   .OrderByDescending(p => p.Value)
  157.                                                                   .ThenBy(n => n.Key).Take(3))
  158.             {
  159.                 Console.WriteLine($"- {topGoalsPair.Key} -> {topGoalsPair.Value}");
  160.             }
  161.         }
  162.  
  163.         private static StringBuilder EscapeTheCharsInThePatt(string enteredPattern)
  164.         {
  165.             StringBuilder strToReturn = new StringBuilder();
  166.  
  167.             for (int currElement = 0; currElement < enteredPattern.Length; currElement++)
  168.             {
  169.                 strToReturn.Append('\\');
  170.                 strToReturn.Append(enteredPattern[currElement]);
  171.             }
  172.  
  173.             return strToReturn;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment