Advertisement
Kamend1

06. Range Day

Jan 21st, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. def starting_position(matrix: list):
  2.     row_idx = 0
  3.     col_idx = 0
  4.     for i in range(5):
  5.         for j in range(5):
  6.             if matrix[i][j] == 'A':
  7.                 row_idx, col_idx = i, j
  8.     return row_idx, col_idx
  9.  
  10.  
  11. def count_targets(matrix):
  12.     counter = 0
  13.     for idx in range(5):
  14.         for col_idx in range(5):
  15.             if matrix[idx][col_idx] == 'x':
  16.                 counter += 1
  17.     return counter
  18.  
  19.  
  20. def shooter_move(row_idx, col_idx, matrix, command, steps):
  21.     for _ in range(steps):
  22.         matrix[row_idx][col_idx] = '.'
  23.         row_idx += directions[command][0]
  24.         col_idx += directions[command][1]
  25.         if row_idx < 0 or row_idx == 5 or col_idx < 0 or col_idx == 5 or matrix[row_idx][col_idx] == 'x':
  26.             row_idx -= directions[command][0]
  27.             col_idx -= directions[command][1]
  28.             matrix[row_idx][col_idx] = 'A'
  29.             break
  30.         else:
  31.             matrix[row_idx][col_idx] = 'A'
  32.     return row_idx, col_idx
  33.  
  34.  
  35. def shoot(row_idx, col_idx, matrix, command, counter):
  36.     row_idx += directions[command][0]
  37.     col_idx += directions[command][1]
  38.     while True:
  39.         if row_idx < 0 or row_idx == 5 or col_idx < 0 or col_idx == 5:
  40.             break
  41.         if matrix[row_idx][col_idx] == 'x':
  42.             counter -= 1
  43.             matrix[row_idx][col_idx] = '.'
  44.             targets_hit.append([row_idx, col_idx])
  45.             break
  46.         row_idx += directions[command][0]
  47.         col_idx += directions[command][1]
  48.     return counter
  49.  
  50.  
  51. rows, cols = 5, 5
  52. shooting_range = [[x for x in input().split()] for _ in range(rows)]
  53. number_commands = int(input())
  54. targets_hit = []
  55.  
  56. directions = {
  57.     'left': [0, -1],
  58.     'right': [0, +1],
  59.     'up': [-1, 0],
  60.     'down': [+1, 0],
  61. }
  62.  
  63. current_row, current_col = starting_position(shooting_range)
  64. number_of_targets = count_targets(shooting_range)
  65.  
  66. for _ in range(number_commands):
  67.     user_command = input().split()
  68.     if user_command[0] == 'move':
  69.         direction, num_steps = user_command[1], int(user_command[2])
  70.         current_row, current_col = shooter_move(current_row, current_col, shooting_range, direction, num_steps)
  71.     elif user_command[0] == 'shoot':
  72.         direction = user_command[1]
  73.         number_of_targets = shoot(current_row, current_col, shooting_range, direction, number_of_targets)
  74.  
  75. if number_of_targets == 0:
  76.     print(f'Training completed! All {len(targets_hit)} targets hit.')
  77. else:
  78.     print(f'Training not completed! {number_of_targets} targets left.')
  79. for target in targets_hit:
  80.     print(target)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement