Advertisement
go6odn28

7_present_delivery.py

Apr 25th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. def is_valid_index(matrix, idx):
  2.     return 0 <= idx < len(matrix)
  3.  
  4.  
  5. def next_move(matrix, curr_row, curr_col, line):
  6.     all_directions = {"up": [curr_row - 1, curr_col],
  7.                       "down": [curr_row + 1, curr_col],
  8.                       "right": [curr_row, curr_col + 1],
  9.                       "left": [curr_row, curr_col - 1]}
  10.  
  11.     new_row, new_col = all_directions[line][0], all_directions[line][1]
  12.     if is_valid_index(matrix, new_row) \
  13.             and is_valid_index(matrix, new_col):
  14.         return new_row, new_col
  15.  
  16.     return None, None
  17.  
  18.  
  19. def fill_the_matrix_and_take_positions(n, s_row, s_col):
  20.     matrix = []
  21.     nice_kids = 0
  22.     for r in range(n):
  23.         row = input().split()
  24.         matrix.append(row)
  25.         if "S" in row:
  26.             s_row = r
  27.             s_col = row.index("S")
  28.         if "V" in row:
  29.             nice_kids += row.count("V")
  30.     return matrix, s_row, s_col, nice_kids
  31.  
  32.  
  33. def cookies_time(matrix, presents_, nice_kids, new_row, new_col):
  34.     surrounding_houses = [
  35.         [new_row - 1, new_col],
  36.         [new_row + 1, new_col],
  37.         [new_row, new_col - 1],
  38.         [new_row, new_col + 1]
  39.     ]
  40.  
  41.     for house in surrounding_houses:
  42.         if presents_ == 0:
  43.             break
  44.         h_row, h_col = house
  45.         if matrix[h_row][h_col] == "X":
  46.             presents_ -= 1
  47.         elif matrix[h_row][h_col] == "V":
  48.             presents_ -= 1
  49.             nice_kids -= 1
  50.  
  51.         matrix[h_row][h_col] = "S_C"  # Check where Santa eat cookie
  52.         matrix[h_row][h_col] = "-"
  53.  
  54.     return matrix, presents_, nice_kids
  55.  
  56.  
  57. def print_result(neighbourhood, nice_kids_left, nice_kids_count, presents):
  58.     if nice_kids_left > 0 and presents == 0:
  59.         print("Santa ran out of presents!")
  60.     [print(" ".join(element)) for element in neighbourhood]
  61.  
  62.     print(f"Good job, Santa! {nice_kids_count} happy nice kid/s.") \
  63.         if nice_kids_left == 0 else print(f"No presents for {nice_kids_left} nice kid/s.")
  64.  
  65.  
  66. def main():
  67.     presents = int(input())
  68.     size = int(input())
  69.     santa_row, santa_col = 0, 0
  70.     neighbourhood, santa_row, santa_col, nice_kids_count = (
  71.         fill_the_matrix_and_take_positions(size, santa_row, santa_col))
  72.     nice_kids_left = nice_kids_count
  73.  
  74.     while True:
  75.         if presents == 0:
  76.             break
  77.         command = input()
  78.         if command == "Christmas morning":
  79.             break
  80.  
  81.         direction = command
  82.         next_row, next_col = next_move(neighbourhood, santa_row, santa_col, direction)
  83.  
  84.         if next_row is not None and next_col is not None:
  85.             if neighbourhood[next_row][next_col] == "V":
  86.                 presents -= 1
  87.                 nice_kids_left -= 1
  88.  
  89.             elif neighbourhood[next_row][next_col] == "C":
  90.                 neighbourhood, presents, nice_kids_left = (
  91.                     cookies_time(neighbourhood, presents, nice_kids_left, next_row, next_col))
  92.  
  93.             neighbourhood[santa_row][santa_col] = "-"
  94.             santa_row, santa_col = next_row, next_col
  95.             neighbourhood[santa_row][santa_col] = "S"  # check where is Santa
  96.  
  97.     print_result(neighbourhood, nice_kids_left, nice_kids_count, presents)
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     main()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement