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