Advertisement
Nenogzar

3 - P!rates

Mar 31st, 2024
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. """ Time: 0.050 s """
  2.  
  3. from collections import defaultdict
  4. # import sys
  5.  
  6. def add_city(city_name, population, gold, cities):
  7.     cities[city_name]['population'] += population
  8.     cities[city_name]['gold'] += gold
  9.  
  10. def plunder_city(city_name, people, gold, cities):
  11.     print(f"{city_name} plundered! {gold} gold stolen, {people} citizens killed.")
  12.     cities[city_name]['population'] -= people
  13.     cities[city_name]['gold'] -= gold
  14.  
  15.     if cities[city_name]['population'] <= 0 or cities[city_name]['gold'] <= 0:
  16.         print(f"{city_name} has been wiped off the map!")
  17.         cities.pop(city_name)
  18.  
  19. def prosper_city(city_name, gold, cities):
  20.     if gold < 0:
  21.         print(f"Gold added cannot be a negative number!")
  22.     else:
  23.         cities[city_name]['gold'] += gold
  24.         print(f"{gold} gold added to the city treasury. {city_name} now has {cities[city_name]['gold']} gold.")
  25.  
  26. def process_commands(command, cities):
  27.     action, city_name = command.split('=>')[0], command.split('=>')[1]
  28.  
  29.     if action == "Plunder":
  30.         people, gold = map(int, command.split('=>')[2:])
  31.         plunder_city(city_name, people, gold, cities)
  32.     elif action == "Prosper":
  33.         gold = int(command.split('=>')[2])
  34.         prosper_city(city_name, gold, cities)
  35.  
  36.  
  37. cities = defaultdict(lambda: {'population': 0, 'gold': 0})
  38. info = input() # sys.stdin.readline().strip()
  39.  
  40. while info != "Sail":
  41.     city_name, population, gold = info.split("||")
  42.     add_city(city_name, int(population), int(gold), cities)
  43.     info = input() # sys.stdin.readline().strip()
  44.  
  45. command = input() # sys.stdin.readline().strip()
  46. while command != "End":
  47.     process_commands(command, cities)
  48.     command = input() # sys.stdin.readline().strip()
  49.  
  50. if cities:
  51.     print(f"Ahoy, Captain! There are {len(cities)} wealthy settlements to go to:")
  52.     for city, city_info in cities.items():
  53.         print(f"{city} -> Population: {city_info['population']} citizens, Gold: {city_info['gold']} kg")
  54. else:
  55.     print(f"Ahoy, Captain! All targets have been plundered and destroyed!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement