Advertisement
alexbancheva

Problem_3._P_rates_FinalExam_04._04._20

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