Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Simulation."""
- from typing import Tuple
- def simulate(world_map: list, flight_plan: list) -> list:
- """
- Simulate a flying space ship fighting space pirates.
- :param world_map: A list of strings indicating rows that make up the space map.
- The space map is always rectangular and the minimum given size is 1x1.
- Space pirate free zone is indicated by the symbol ('-'), low presence by ('w') and high presence by
- ('W').
- The ship position is indicated by the symbol ('X'). There is always one ship on the space map.
- Asteroid fields are indicated by the symbol ('#').
- :param flight_plan: A list of moves.
- The moves are abbreviated N - north, E - east, S - south, W - west.
- Ignore moves that would put the ship out of bounds or crash it into an asteroid field.
- :return: A list of strings indicating rows that make up the space map. Same format as the given wmap.
- Pirates under Shippy's starting position are always eliminated ('-').
- If Shippy fights pirates in high presence area, it first turns into low presence ('w')
- and then from low presence into no presence area ('-').
- """
- coordinates_dict, shippy_y, shippy_x = list_to_dictionary_converter(world_map)
- for i in range(len(flight_plan)):
- if flight_plan[i] == "N" and shippy_y - 1 >= 0 and coordinates_dict[(shippy_y - 1, shippy_x)] != "#":
- shippy_y -= 1
- switch_letters(coordinates_dict, shippy_y, shippy_x)
- if flight_plan[i] == "S" and shippy_y + 1 < len(world_map) \
- and coordinates_dict[(shippy_y + 1, shippy_x)] != "#":
- shippy_y += 1
- switch_letters(coordinates_dict, shippy_y, shippy_x)
- if flight_plan[i] == "E" and shippy_x + 1 < len(world_map[0]) \
- and coordinates_dict[(shippy_y, shippy_x + 1)] != "#":
- shippy_x += 1
- switch_letters(coordinates_dict, shippy_y, shippy_x)
- if flight_plan[i] == "W" and shippy_x - 1 >= 0 and coordinates_dict[(shippy_y, shippy_x - 1)] != "#":
- shippy_x -= 1
- switch_letters(coordinates_dict, shippy_y, shippy_x)
- coordinates_dict[(shippy_y, shippy_x)] = "X"
- return dictionary_to_list_converter(coordinates_dict, len(world_map), len(world_map[0]))
- def list_to_dictionary_converter(world_map: list) -> Tuple[dict, int, int]:
- """
- Convert a list to dictionary using coordinates as keys.
- :param world_map: list of strings.
- :return: dictionary of the space, shippy y position, shippy x position
- Map tile under Shippy's location is marked as "-" or no presence area.
- Dictionaries key is a Tuple which has Y-position as the first value and X-position as
- the second value. If there is no Shippy (Marked as X in the list) in the list, the
- coordinates are marked as 0 and 0.
- """
- coordinates_dict = {}
- shippy_y = 0
- shippy_x = 0
- for i in range(len(world_map)):
- for j in range(len(world_map[i])):
- if world_map[i][j] == "X":
- shippy_y = i
- shippy_x = j
- coordinates_dict[(i, j)] = "-"
- else:
- coordinates_dict[(i, j)] = world_map[i][j]
- return coordinates_dict, shippy_y, shippy_x
- def dictionary_to_list_converter(space_map: dict, height: int, width: int) -> list:
- """
- Convert dictionary of coordinates to list of strings.
- :param space_map: Dictionary of the space
- :param width: Width of the world.
- :param height: Height of the world.
- :return: List of strings.
- PS: You should add Shippy back to the dictionary before you call this method.
- """
- shippy_map = []
- j = 0
- for i in space_map:
- if j % width == 0:
- shippy_map.append(" ")
- shippy_map.append(space_map[i])
- else:
- shippy_map.append(space_map[i])
- j += 1
- return "".join(shippy_map).split()
- def switch_letters(coordinates_dict, shippy_y, shippy_x):
- """Switch letters in map."""
- if coordinates_dict[(shippy_y, shippy_x)] == "W":
- coordinates_dict[(shippy_y, shippy_x)] = "w"
- elif coordinates_dict[(shippy_y, shippy_x)] == "w":
- coordinates_dict[(shippy_y, shippy_x)] = "-"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement