Advertisement
elena1234

P!rates-ExamPreparation

Dec 4th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P!rates
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string command = string.Empty;
  12.             var targetTownPeopleGold = new Dictionary<string, List<int>>();
  13.             while ((command = Console.ReadLine()) != "Sail")
  14.             {
  15.                 string[] commandArray = command.Split("||");
  16.                 string town = commandArray[0];
  17.                 int people = int.Parse(commandArray[1]);
  18.                 int gold = int.Parse(commandArray[2]);
  19.                 if (targetTownPeopleGold.ContainsKey(town) == false)
  20.                 {
  21.                     targetTownPeopleGold[town] = new List<int>();
  22.                     targetTownPeopleGold[town].Add(people);
  23.                     targetTownPeopleGold[town].Add(gold);
  24.                 }
  25.  
  26.                 else if (targetTownPeopleGold.ContainsKey(town) == true)
  27.                 {
  28.                     people = people + targetTownPeopleGold[town][0];
  29.                     gold = gold + targetTownPeopleGold[town][1];
  30.                     targetTownPeopleGold[town][0] = people;
  31.                     targetTownPeopleGold[town][1] = gold;
  32.                 }
  33.             }
  34.  
  35.             string events = string.Empty;
  36.             while ((events = Console.ReadLine()) != "End")
  37.             {
  38.                 string[] eventsArray = events.Split("=>");
  39.                 if (eventsArray[0] == "Plunder")
  40.                 {
  41.                     string town = eventsArray[1];
  42.                     int people = int.Parse(eventsArray[2]);
  43.                     int gold = int.Parse(eventsArray[3]);
  44.                     if (targetTownPeopleGold.ContainsKey(town))
  45.                     {
  46.                         if ((people <= targetTownPeopleGold[town][0]) && (gold <= targetTownPeopleGold[town][1]))
  47.                         {
  48.                             targetTownPeopleGold[town][0] = targetTownPeopleGold[town][0] - people;
  49.                             targetTownPeopleGold[town][1] = targetTownPeopleGold[town][1] - gold;
  50.                             Console.WriteLine($"{town} plundered! {gold} gold stolen, {people} citizens killed.");
  51.                         }
  52.  
  53.                         if (targetTownPeopleGold[town][0] == 0 || targetTownPeopleGold[town][1] == 0)
  54.                         {
  55.                             targetTownPeopleGold.Remove(town);
  56.                             Console.WriteLine($"{town} has been wiped off the map!");
  57.                         }
  58.                     }
  59.                 }
  60.  
  61.                 else if (eventsArray[0] == "Prosper")
  62.                 {
  63.                     string town = eventsArray[1];
  64.                     int gold = int.Parse(eventsArray[2]);
  65.                     if (targetTownPeopleGold.ContainsKey(town) && gold > 0)
  66.                     {
  67.                         targetTownPeopleGold[town][1] = targetTownPeopleGold[town][1] + gold;
  68.                         Console.WriteLine($"{gold} gold added to the city treasury. {town} now has {targetTownPeopleGold[town][1]} gold.");
  69.                     }
  70.  
  71.                     else if (targetTownPeopleGold.ContainsKey(town) && gold < 0)
  72.                     {
  73.                         Console.WriteLine($"Gold added cannot be a negative number!");
  74.                     }
  75.                 }
  76.  
  77.             }
  78.  
  79.             SortAndPrintResult(targetTownPeopleGold);
  80.         }
  81.  
  82.  
  83.         private static void SortAndPrintResult(Dictionary<string, List<int>> targetTownPeopleGold)
  84.         {
  85.             if (targetTownPeopleGold.Keys.Count > 0)
  86.             {
  87.                 Console.WriteLine($"Ahoy, Captain! There are {targetTownPeopleGold.Values.Count} wealthy settlements to go to:");
  88.                 foreach (var kvp in targetTownPeopleGold.OrderByDescending(x => x.Value[1]).ThenBy(x => x.Key))
  89.                 {
  90.                     string town = kvp.Key;
  91.                     int people = kvp.Value[0];
  92.                     int gold = kvp.Value[1];
  93.                     Console.WriteLine($"{town} -> Population: {people} citizens, Gold: {gold} kg");
  94.                 }
  95.             }
  96.  
  97.             else if (targetTownPeopleGold.Keys.Count == 0)
  98.             {
  99.                 Console.WriteLine($"Ahoy, Captain! All targets have been plundered and destroyed!");
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement