Advertisement
Guest User

Untitled

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