Advertisement
GeorgiLukanov87

07. Present Delivery - Multidimensional Lists - Exercise 2

Sep 4th, 2022 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. #    07. Present Delivery
  2.  
  3. #    Multidimensional Lists - Exercise 2
  4. #    https://judge.softuni.org/Contests/Practice/Index/3194#6
  5.  
  6.  
  7. def is_inside(some_row, some_col, original_size):  # validating if cell is inside the matrix.
  8.     return 0 <= some_row < original_size and 0 <= some_col < original_size
  9.  
  10.  
  11. def give_next_position(direction, row, col):
  12.     if direction == 'up':
  13.         return row - 1, col
  14.     elif direction == 'down':
  15.         return row + 1, col
  16.     elif direction == 'left':
  17.         return row, col - 1
  18.     elif direction == 'right':
  19.         return row, col + 1
  20.  
  21.  
  22. def get_around_kids(row, col):  # Get all kids indexes in a LIST , which surrounding Santa's position.
  23.     all_kids = []
  24.     directions = ['left', 'right', 'up', 'down']  # The EXACT ORDER !
  25.     for position in range(len(directions)):
  26.         all_kids.append(give_next_position(directions[position], row, col))
  27.     return all_kids
  28.  
  29.  
  30. gifts = int(input())
  31. size = int(input())
  32. matrix = []
  33.  
  34. total_nice_kids = 0
  35. given_gifts = 0
  36.  
  37. santa_row = 0
  38. santa_col = 0
  39.  
  40. for row in range(size):
  41.     matrix.append(input().split())
  42.     for col in range(size):
  43.         if matrix[row][col] == 'S':  # Extracting Santa's position.
  44.             santa_row = row
  45.             santa_col = col
  46.         elif matrix[row][col] == 'V':  # Finding count of good kids in the hood.
  47.             total_nice_kids += 1
  48.  
  49. while True:
  50.     command = input()
  51.     if command == 'Christmas morning':
  52.         break
  53.  
  54.     next_row, next_col = give_next_position(command, santa_row, santa_col)
  55.  
  56.     if is_inside(next_row, next_col, size):
  57.         matrix[santa_row][santa_col] = '-'  # Clear old Santa's position !
  58.         santa_row = next_row
  59.         santa_col = next_col
  60.     else:
  61.         continue
  62.  
  63.     if matrix[santa_row][santa_col] == 'V':
  64.         gifts -= 1
  65.         total_nice_kids -= 1
  66.         given_gifts += 1
  67.  
  68.     elif matrix[santa_row][santa_col] == 'C':  # Cookie mode ON !
  69.         around_kids = get_around_kids(santa_row, santa_col)
  70.         for kid in around_kids:
  71.             current_kid = matrix[kid[0]][kid[1]]
  72.             if current_kid != '-' and gifts:
  73.                 if current_kid == 'V':
  74.                     gifts -= 1
  75.                     total_nice_kids -= 1
  76.                     given_gifts += 1
  77.                 elif current_kid == 'X':
  78.                     gifts -= 1
  79.  
  80.             matrix[kid[0]][kid[1]] = '-'  # Clear kid position , after getin gift.
  81.  
  82.             if gifts == 0 and total_nice_kids:
  83.                 break
  84.  
  85.     matrix[santa_row][santa_col] = 'S'  # Move Santa to new position.
  86.  
  87.     if gifts == 0 and total_nice_kids:  # if gifts = 0 , but still good kid without gift.
  88.         print('Santa ran out of presents!')
  89.         break
  90.  
  91.     if gifts == 0:  # End program , if out of gifts.
  92.         break
  93.  
  94. for el in matrix:
  95.     print(*el)
  96.  
  97. if total_nice_kids == 0:
  98.     print(f'Good job, Santa! {given_gifts} happy nice kid/s. ')
  99. else:
  100.     print(f'No presents for {total_nice_kids} nice kid/s.')
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement