Advertisement
Guest User

Untitled

a guest
Jan 10th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Linq;
  5.  
  6. namespace MOBAChallenger_withClass
  7. {
  8.     public class Program
  9.     {
  10.         private static void Main()
  11.         {
  12.             var dictNamePlayer = new Dictionary<string, Player>();
  13.             string command;
  14.             while ((command = Console.ReadLine()) != "Season end")
  15.             {
  16.                 if (!command.Contains("vs"))
  17.                 {
  18.                     var commandArray = command.Split(" -> ");
  19.                     var playerName = commandArray[0];
  20.                     var position = commandArray[1];
  21.                     var skill = int.Parse(commandArray[2]);
  22.  
  23.                     if (!dictNamePlayer.ContainsKey(playerName))
  24.                     {
  25.                         var newPlayer = new Player(playerName);
  26.                         dictNamePlayer.Add(playerName, newPlayer);
  27.                     }
  28.  
  29.                     dictNamePlayer[playerName].AddOrUpdatePosition(position, skill);
  30.                 }
  31.                 else
  32.                 {
  33.                     var commandArray = command.Split(' ');
  34.                     var firstPlayer = commandArray[0];
  35.                     var secondPlayer = commandArray[2];
  36.  
  37.                     if (!dictNamePlayer.ContainsKey(firstPlayer) || !dictNamePlayer.ContainsKey(secondPlayer))
  38.                     {
  39.                         continue;
  40.                     }
  41.  
  42.                     var playerOne = dictNamePlayer[firstPlayer];
  43.                     var playerTwo = dictNamePlayer[secondPlayer];
  44.  
  45.                     if (playerOne.GetTotalSkillPoints() == playerTwo.GetTotalSkillPoints())
  46.                     {
  47.                         continue;
  48.                     }
  49.  
  50.                     var duelsCount = playerOne.GetPositions()
  51.                         .Intersect(playerTwo.GetPositions())
  52.                         .Count();
  53.  
  54.                     if (duelsCount <= 0)
  55.                     {
  56.                         continue;
  57.                     }
  58.  
  59.                     dictNamePlayer.Remove(playerOne.GetTotalSkillPoints() > playerTwo.GetTotalSkillPoints()
  60.                         ? playerTwo.Name
  61.                         : playerOne.Name);
  62.                 }
  63.             }
  64.  
  65.             PrintResult(dictNamePlayer.Values.ToList());
  66.         }
  67.  
  68.         private static void PrintResult(IEnumerable<Player> players)
  69.         {
  70.             foreach (var player in players
  71.                 .OrderByDescending(player => player.GetTotalSkillPoints())
  72.                 .ThenBy(player => player.Name))
  73.             {
  74.                 Console.WriteLine($"{player.Name}: {player.GetTotalSkillPoints()} skill");
  75.  
  76.                 foreach (var (position, skill) in player
  77.                     .GetPositionsAndSkillsDictionary()
  78.                     .OrderByDescending(x => x.Value)
  79.                     .ThenBy(x => x.Key))
  80.                 {
  81.                     Console.WriteLine($"- {position} <::> {skill}");
  82.                 }
  83.             }
  84.         }
  85.  
  86.         private class Player
  87.         {
  88.             public string Name { get; }
  89.  
  90.             private readonly Dictionary<string, int> _dictPositionSkill;
  91.  
  92.             public Player(string name)
  93.             {
  94.                 Name = name;
  95.                 _dictPositionSkill = new Dictionary<string, int>();
  96.             }
  97.  
  98.             public void AddOrUpdatePosition(string position, int skill)
  99.             {
  100.                 if (!_dictPositionSkill.ContainsKey(position))
  101.                 {
  102.                     _dictPositionSkill.Add(position, skill);
  103.                 }
  104.                 else if (_dictPositionSkill[position] < skill)
  105.                 {
  106.                     _dictPositionSkill[position] = skill;
  107.                 }
  108.             }
  109.  
  110.             public IEnumerable<string> GetPositions()
  111.             {
  112.                 return _dictPositionSkill.Keys;
  113.             }
  114.  
  115.             public ImmutableDictionary<string, int> GetPositionsAndSkillsDictionary()
  116.             {
  117.                 return _dictPositionSkill.ToImmutableDictionary();
  118.             }
  119.  
  120.             public int GetTotalSkillPoints()
  121.             {
  122.                 return _dictPositionSkill.Values.Sum();
  123.             }
  124.         }
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement