IPetrov007

WWParty

May 15th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 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 _04_WormsWorldParty
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var pattern = @"^([a-zA-Z0-9 ]+) -> ([a-zA-Z0-9 ]+) -> ([0-9]+)$";
  15. Regex regex = new Regex(pattern);
  16.  
  17. var names = new List<string>();
  18. var teams = new Dictionary<string, Dictionary<string, long>>();
  19. while (true)
  20. {
  21. var input = Console.ReadLine();
  22. if (input == "quit")
  23. {
  24. break;
  25. }
  26. var matches = regex.Match(input);
  27. var wormName = matches.Groups[1].Value;
  28. var teamName = matches.Groups[2].Value;
  29. var wormScore = long.Parse(matches.Groups[3].Value);
  30.  
  31. if (names.Contains(wormName))
  32. {
  33. continue;
  34. }
  35. else
  36. {
  37. names.Add(wormName);
  38. }
  39. if (!teams.ContainsKey(teamName))
  40. {
  41. teams[teamName] = new Dictionary<string, long>();
  42. }
  43. if (!teams[teamName].ContainsKey(wormName))
  44. {
  45. teams[teamName].Add(wormName, wormScore);
  46. }
  47. else
  48. {
  49. teams[teamName][wormName] += wormScore;
  50. }
  51. }
  52.  
  53. teams = teams
  54. .OrderByDescending(x => x.Value.Values.Sum())
  55. .ThenByDescending(x => x.Value.Values.Average())
  56. .ToDictionary(x => x.Key, x => x.Value);
  57.  
  58. var teamCount = 1;
  59. foreach (var team in teams)
  60. {
  61. Console.WriteLine($"{teamCount}. Team: {team.Key} - {team.Value.Values.Sum()}");
  62. foreach (var worm in team.Value.OrderByDescending(x => x.Value))
  63. {
  64. Console.WriteLine($"###{worm.Key} : {worm.Value}");
  65. }
  66. teamCount++;
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment