Advertisement
vesso8

Targets

Jun 27th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. def is_in_range(row, col , n):
  2.     if 0 <= row < n and 0 <= col < n:
  3.         return True
  4.     return False
  5.  
  6. N = 5
  7. MY_POSITION = "A"
  8. TARGETS = "x"
  9. matrix = []
  10. current_position = ()
  11. targets_count = 0
  12. total_targets = 0
  13. for row_index in range(N):
  14.     current_row = [el for el in input().split()]
  15.     if MY_POSITION in current_row:
  16.         column_index = current_row.index(MY_POSITION)
  17.         current_position = (row_index, column_index)
  18.     if TARGETS in current_row:
  19.         for count in current_row:
  20.             if TARGETS == count:
  21.                 targets_count += 1
  22.     matrix.append(current_row)
  23.  
  24. directions = {
  25.     "up": (-1, 0),
  26.     "down": (1, 0),
  27.     "left": (0, -1),
  28.     "right": (0, 1)
  29. }
  30.  
  31. number_of_commands = int(input())
  32. targets_hit_position = []
  33. training_completed = False
  34. for _ in range(number_of_commands):
  35.     if targets_count <= 0:
  36.         training_completed = True
  37.         break
  38.     command = input().split()
  39.     current_row = int(current_position[0])
  40.     current_col = int(current_position[1])
  41.     if command[0] == "shoot":
  42.         position = command[1]
  43.         for shooting in directions:
  44.             if shooting == position:
  45.                 next_row = current_row + directions[shooting][0]
  46.                 next_col = current_col + directions[shooting][1]
  47.                 while is_in_range(next_row, next_col, N):
  48.                     if matrix[next_row][next_col] == "x":
  49.                         targets_hit_position.append([next_row, next_col])
  50.                         matrix[next_row][next_col] = "."
  51.                         targets_count -= 1
  52.                         total_targets += 1
  53.                         if targets_count <= 0:
  54.                             training_completed = True
  55.                             break
  56.                         break
  57.                     next_row += directions[shooting][0]
  58.                     next_col += directions[shooting][1]
  59.                 break
  60.     elif command[0] == "move":
  61.         position = command[1]
  62.         steps = int(command[2])
  63.         for moving in directions:
  64.             if moving == position:
  65.                 potential_row = current_row + directions[moving][0]
  66.                 potential_col = current_col + directions[moving][1]
  67.                 for moves in range(steps-1):
  68.                     if is_in_range(potential_row, potential_col , N):
  69.                         if matrix[potential_row][potential_col] != "x":
  70.                             potential_row += directions[moving][0]
  71.                             potential_col += directions[moving][1]
  72.                         else:
  73.                             break
  74.                 if is_in_range(potential_row, potential_col, N):
  75.                     if matrix[potential_row][potential_col] != "x":
  76.                         matrix[current_row][current_col] = "."
  77.                         current_row = potential_row
  78.                         current_col = potential_col
  79.                         matrix[current_row][current_col] = "A"
  80.                     else:
  81.                         break
  82. if training_completed:
  83.     print(f"Training completed! All {total_targets} targets hit.")
  84. else:
  85.     print(f"Training not completed! {targets_count} targets left.")
  86. print('\n'.join([str(el) for el in targets_hit_position]))
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement