Advertisement
go6odn28

07. Present Delivery_2

Jun 1st, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. number_of_presents = int(input())
  2. size_of_matrix = int(input())
  3.  
  4. nice_kids = []
  5. santa_current_position = (0, 0)
  6.  
  7. matrix = []
  8.  
  9. for row in range(size_of_matrix):
  10.     lanes = [x for x in input().split()]
  11.     matrix.append(lanes)
  12.     for col in range(size_of_matrix):
  13.         if matrix[row][col] == 'V':
  14.             nice_kids.append([row, col])
  15.         elif matrix[row][col] == 'S':
  16.             santa_current_position = (row, col)
  17.  
  18. directions = {
  19.     'up': (-1, 0),
  20.     'down': (1, 0),
  21.     'right': (0, 1),
  22.     'left': (0, -1)
  23. }
  24. happy_kids = len(nice_kids)
  25. while True:
  26.  
  27.     if number_of_presents <= 0:
  28.         break
  29.  
  30.     commands = input()
  31.     if commands == 'Christmas morning':
  32.         break
  33.  
  34.     next_row = santa_current_position[0] + directions[commands][0]
  35.     next_col = santa_current_position[1] + directions[commands][1]
  36.  
  37.     if 0 <= next_row < size_of_matrix and 0 <= next_col < size_of_matrix:
  38.         matrix[santa_current_position[0]][santa_current_position[1]] = '-'
  39.  
  40.         if matrix[next_row][next_col] == '-' or matrix[next_row][next_col] == 'X':
  41.             santa_current_position = (next_row, next_col)
  42.             continue
  43.         elif matrix[next_row][next_col] == 'V':
  44.             number_of_presents -= 1
  45.             nice_kids.remove([next_row, next_col])
  46.  
  47.         elif matrix[next_row][
  48.             next_col] == 'C':
  49.  
  50.             if matrix[next_row - 1][next_col] == 'V' or matrix[next_row - 1][next_col] == 'X':
  51.                 number_of_presents -= 1
  52.  
  53.                 nice_kids.remove([next_row - 1, next_col]) if matrix[next_row - 1][next_col] == 'V' else ''
  54.                 matrix[next_row - 1][next_col] = '-'
  55.  
  56.             if matrix[next_row + 1][next_col] == 'V' or matrix[next_row + 1][next_col] == 'X':
  57.                 number_of_presents -= 1
  58.  
  59.                 nice_kids.remove([next_row + 1, next_col]) if matrix[next_row + 1][next_col] == 'V' else ''
  60.                 matrix[next_row + 1][next_col] = '-'
  61.  
  62.             if matrix[next_row][next_col - 1] == 'V' or matrix[next_row][next_col - 1] == 'X':
  63.                 number_of_presents -= 1
  64.  
  65.                 nice_kids.remove([next_row, next_col - 1]) if matrix[next_row][next_col - 1] == 'V' else ''
  66.                 matrix[next_row][next_col - 1] = '-'
  67.  
  68.             if matrix[next_row][next_col + 1] == 'V' or matrix[next_row][next_col + 1] == 'X':
  69.                 number_of_presents -= 1
  70.  
  71.                 nice_kids.remove([next_row, next_col + 1]) if matrix[next_row][next_col + 1] == 'V' else ''
  72.                 matrix[next_row][next_col + 1] = '-'
  73.  
  74.         santa_current_position = (next_row, next_col)
  75.         matrix[santa_current_position[0]][santa_current_position[1]] = 'S'
  76.  
  77.  
  78. if nice_kids and number_of_presents == 0:
  79.     print("Santa ran out of presents!")
  80. [print(" ".join(element)) for element in matrix]
  81.  
  82. print(f"Good job, Santa! {happy_kids} happy nice kid/s.") \
  83.     if not nice_kids else print(f"No presents for {len(nice_kids)} nice kid/s.")
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement