Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace P!rates
- {
- class Program
- {
- static void Main(string[] args)
- {
- string command = string.Empty;
- var targetTownPeopleGold = new Dictionary<string, List<int>>();
- while ((command = Console.ReadLine()) != "Sail")
- {
- string[] commandArray = command.Split("||");
- string town = commandArray[0];
- int people = int.Parse(commandArray[1]);
- int gold = int.Parse(commandArray[2]);
- if (targetTownPeopleGold.ContainsKey(town) == false)
- {
- targetTownPeopleGold[town] = new List<int>();
- targetTownPeopleGold[town].Add(people);
- targetTownPeopleGold[town].Add(gold);
- }
- else if (targetTownPeopleGold.ContainsKey(town) == true)
- {
- people = people + targetTownPeopleGold[town][0];
- gold = gold + targetTownPeopleGold[town][1];
- targetTownPeopleGold[town][0] = people;
- targetTownPeopleGold[town][1] = gold;
- }
- }
- string events = string.Empty;
- while ((events = Console.ReadLine()) != "End")
- {
- string[] eventsArray = events.Split("=>");
- if (eventsArray[0] == "Plunder")
- {
- string town = eventsArray[1];
- int people = int.Parse(eventsArray[2]);
- int gold = int.Parse(eventsArray[3]);
- if (targetTownPeopleGold.ContainsKey(town))
- {
- if ((people <= targetTownPeopleGold[town][0]) && (gold <= targetTownPeopleGold[town][1]))
- {
- targetTownPeopleGold[town][0] = targetTownPeopleGold[town][0] - people;
- targetTownPeopleGold[town][1] = targetTownPeopleGold[town][1] - gold;
- Console.WriteLine($"{town} plundered! {gold} gold stolen, {people} citizens killed.");
- }
- if (targetTownPeopleGold[town][0] == 0 || targetTownPeopleGold[town][1] == 0)
- {
- targetTownPeopleGold.Remove(town);
- Console.WriteLine($"{town} has been wiped off the map!");
- }
- }
- }
- else if (eventsArray[0] == "Prosper")
- {
- string town = eventsArray[1];
- int gold = int.Parse(eventsArray[2]);
- if (targetTownPeopleGold.ContainsKey(town) && gold > 0)
- {
- targetTownPeopleGold[town][1] = targetTownPeopleGold[town][1] + gold;
- Console.WriteLine($"{gold} gold added to the city treasury. {town} now has {targetTownPeopleGold[town][1]} gold.");
- }
- else if (targetTownPeopleGold.ContainsKey(town) && gold < 0)
- {
- Console.WriteLine($"Gold added cannot be a negative number!");
- }
- }
- }
- SortAndPrintResult(targetTownPeopleGold);
- }
- private static void SortAndPrintResult(Dictionary<string, List<int>> targetTownPeopleGold)
- {
- if (targetTownPeopleGold.Keys.Count > 0)
- {
- Console.WriteLine($"Ahoy, Captain! There are {targetTownPeopleGold.Values.Count} wealthy settlements to go to:");
- foreach (var kvp in targetTownPeopleGold.OrderByDescending(x => x.Value[1]).ThenBy(x => x.Key))
- {
- string town = kvp.Key;
- int people = kvp.Value[0];
- int gold = kvp.Value[1];
- Console.WriteLine($"{town} -> Population: {people} citizens, Gold: {gold} kg");
- }
- }
- else if (targetTownPeopleGold.Keys.Count == 0)
- {
- Console.WriteLine($"Ahoy, Captain! All targets have been plundered and destroyed!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement