Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 07. Present Delivery
- # Multidimensional Lists - Exercise 2
- # https://judge.softuni.org/Contests/Practice/Index/3194#6
- def is_inside(some_row, some_col, original_size): # validating if cell is inside the matrix.
- return 0 <= some_row < original_size and 0 <= some_col < original_size
- def give_next_position(direction, row, col):
- if direction == 'up':
- return row - 1, col
- elif direction == 'down':
- return row + 1, col
- elif direction == 'left':
- return row, col - 1
- elif direction == 'right':
- return row, col + 1
- def get_around_kids(row, col): # Get all kids indexes in a LIST , which surrounding Santa's position.
- all_kids = []
- directions = ['left', 'right', 'up', 'down'] # The EXACT ORDER !
- for position in range(len(directions)):
- all_kids.append(give_next_position(directions[position], row, col))
- return all_kids
- gifts = int(input())
- size = int(input())
- matrix = []
- total_nice_kids = 0
- given_gifts = 0
- santa_row = 0
- santa_col = 0
- for row in range(size):
- matrix.append(input().split())
- for col in range(size):
- if matrix[row][col] == 'S': # Extracting Santa's position.
- santa_row = row
- santa_col = col
- elif matrix[row][col] == 'V': # Finding count of good kids in the hood.
- total_nice_kids += 1
- while True:
- command = input()
- if command == 'Christmas morning':
- break
- next_row, next_col = give_next_position(command, santa_row, santa_col)
- if is_inside(next_row, next_col, size):
- matrix[santa_row][santa_col] = '-' # Clear old Santa's position !
- santa_row = next_row
- santa_col = next_col
- else:
- continue
- if matrix[santa_row][santa_col] == 'V':
- gifts -= 1
- total_nice_kids -= 1
- given_gifts += 1
- elif matrix[santa_row][santa_col] == 'C': # Cookie mode ON !
- around_kids = get_around_kids(santa_row, santa_col)
- for kid in around_kids:
- current_kid = matrix[kid[0]][kid[1]]
- if current_kid != '-' and gifts:
- if current_kid == 'V':
- gifts -= 1
- total_nice_kids -= 1
- given_gifts += 1
- elif current_kid == 'X':
- gifts -= 1
- matrix[kid[0]][kid[1]] = '-' # Clear kid position , after getin gift.
- if gifts == 0 and total_nice_kids:
- break
- matrix[santa_row][santa_col] = 'S' # Move Santa to new position.
- if gifts == 0 and total_nice_kids: # if gifts = 0 , but still good kid without gift.
- print('Santa ran out of presents!')
- break
- if gifts == 0: # End program , if out of gifts.
- break
- for el in matrix:
- print(*el)
- if total_nice_kids == 0:
- print(f'Good job, Santa! {given_gifts} happy nice kid/s. ')
- else:
- print(f'No presents for {total_nice_kids} nice kid/s.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement