Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def next_move(rows, row, col, direction):
- moves = {'up': (-1, 0),
- 'down': (1, 0),
- 'left': (0, -1),
- 'right': (0, 1)}
- d_row, d_col = moves[direction]
- row = (row + d_row) % rows
- col = (col + d_col) % rows
- return row, col
- def fill_the_matrix_and_find_ship_position(rows, row, col):
- matrix = []
- for idx in range(rows):
- r = list(input())
- matrix.append(r)
- if 'S' in r:
- row = idx
- col = r.index('S')
- return matrix, row, col
- def whirlpool_check(matrix, row, col):
- return matrix[row][col] == 'W'
- def mark_past_moves(field, ship_row, ship_col, next_row, next_col):
- field[ship_row][ship_col] = '-'
- ship_row, ship_col = next_row, next_col
- field[ship_row][ship_col] = 'S'
- return field, ship_row, ship_col, next_row, next_col
- def fish_check(field, next_row, next_col):
- return field[next_row][next_col].isdigit()
- def print_result(field, fish_amount, quota_goal, fall_in_whirlpool, last_coordinates):
- result = ''
- if fall_in_whirlpool:
- last_row, last_col = last_coordinates[0], last_coordinates[1]
- result += (f'You fell into a whirlpool! '
- f'The ship sank and you lost the fish you caught. '
- f'Last coordinates of the ship: [{last_row},{last_col}]\n')
- else:
- if fish_amount >= quota_goal:
- result += 'Success! You managed to reach the quota!\n'
- else:
- lack_of_fish = quota_goal - fish_amount
- result += (f"You didn't catch enough fish and didn't reach the quota! "
- f"You need {lack_of_fish} tons of fish more.\n")
- result += f'Amount of fish caught: {fish_amount} tons.\n' if fish_amount > 0 else ''
- for row in field:
- string_row = ''.join(row)
- result += f"{string_row}\n"
- result.strip()
- return print(result)
- def main():
- fish_amount = 0
- ship_row, ship_col = 0, 0
- fall_in_whirlpool = False
- quota_goal = 20
- last_coordinates = None
- rows = int(input())
- field, ship_row, ship_col = fill_the_matrix_and_find_ship_position(rows, ship_row, ship_col)
- while True:
- command = input()
- if command == 'collect the nets':
- break
- direction = command
- next_row, next_col = next_move(rows, ship_row, ship_col, direction)
- fall_in_whirlpool = whirlpool_check(field, next_row, next_col)
- if fall_in_whirlpool:
- last_coordinates = next_row, next_col
- break
- have_fish = fish_check(field, next_row, next_col)
- if have_fish:
- amount = int(field[next_row][next_col])
- fish_amount += amount
- field, ship_row, ship_col, next_row, next_col = (
- mark_past_moves(field, ship_row, ship_col, next_row, next_col))
- print_result(field, fish_amount, quota_goal, fall_in_whirlpool, last_coordinates)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement