Advertisement
Nenogzar

2

Jun 18th, 2024
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. def directions(position, direction, rows, matrix):
  2.     row, col = position
  3.     matrix[row][col] = "-"
  4.  
  5.     if direction == "up":
  6.         row = (row - 1) % rows
  7.     elif direction == "down":
  8.         row = (row + 1) % rows
  9.     elif direction == "left":
  10.         col = (col - 1) % rows
  11.     elif direction == "right":
  12.         col = (col + 1) % rows
  13.  
  14.     return row, col
  15.  
  16.  
  17. def print_matrix(matrix):
  18.     for row in matrix:
  19.         print("".join(row))
  20.  
  21.  
  22. rows = int(input())
  23. fishing_area = []
  24. captain, whirlpool, empty, passage, quota = "S", "W", "-", 0, 20
  25. position = None
  26.  
  27. for row_ in range(rows):
  28.     line = list(input())
  29.     if captain in line:
  30.         position = (row_, line.index(captain))
  31.     fishing_area.append(line)
  32.  
  33. direction = input()
  34.  
  35. while direction != "collect the nets":
  36.     new_position = directions(position, direction, rows,fishing_area)
  37.     n_row, n_col = new_position
  38.  
  39.     step = fishing_area[n_row][n_col]
  40.  
  41.     if step.isdigit():
  42.         passage += int(step)
  43.  
  44.     if step == whirlpool:
  45.         passage = 0
  46.         print(f"You fell into a whirlpool! The ship sank and you lost the fish you caught. "
  47.               f"Last coordinates of the ship: [{n_row},{n_col}]")
  48.         exit()
  49.  
  50.     fishing_area[n_row][n_col] = captain
  51.     position = new_position
  52.     direction = input()
  53.  
  54. if passage >= quota:
  55.     print(f"Success! You managed to reach the quota!")
  56. else:
  57.     print(f"You didn't catch enough fish and didn't reach the quota! You need {quota - passage} tons of fish more.")
  58. if passage > 0:
  59.     print(f"Amount of fish caught: {passage} tons.")
  60.  
  61. print_matrix(fishing_area)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement