Advertisement
viligen

present_delivery

Jan 25th, 2022
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. def next_move(p_r, p_c, direction_, steps_):
  2.     new_p_r, new_p_c = p_r, p_c
  3.     if direction_ == 'up':
  4.         new_p_r -= steps_
  5.     elif direction_ == 'down':
  6.         new_p_r += steps_
  7.     elif direction_ == "left":
  8.         new_p_c -= steps_
  9.     elif direction_ == "right":
  10.         new_p_c += steps_
  11.     return new_p_r, new_p_c
  12.  
  13.  
  14. def valid_idx(r, c, size_):
  15.     return 0 <= r < size_ and 0 <= c < size_
  16.  
  17.  
  18. presents = int(input())
  19. size = int(input())
  20. matrix = []
  21. total_good_kids = 0
  22. total_delivered_to_good_kids = 0
  23. santa_row, santa_col = None, None
  24.  
  25. for row in range(size):
  26.     current_row = input().split()
  27.     matrix.append(current_row)
  28.     for col in range(size):
  29.         if matrix[row][col] == "S":
  30.             santa_row, santa_col = row, col
  31.         elif matrix[row][col] == "V":
  32.             total_good_kids += 1
  33.  
  34. while True:
  35.  
  36.     direction = input()
  37.     if direction == 'Christmas morning':
  38.         break
  39.  
  40.     matrix[santa_row][santa_col] = '-'
  41.     next_row, next_col = next_move(santa_row, santa_col, direction, 1)
  42.  
  43.     if not valid_idx(next_row, next_col, size):
  44.         continue
  45.  
  46.     if matrix[next_row][next_col] == 'V':
  47.         total_delivered_to_good_kids += 1
  48.         presents -= 1
  49.  
  50.     elif matrix[next_row][next_col] == 'C':
  51.  
  52.         if matrix[next_row][next_col-1] not in ['-', 'C'] and presents:
  53.             presents -= 1
  54.             if matrix[next_row][next_col - 1] == 'V':
  55.                 total_delivered_to_good_kids += 1
  56.             matrix[next_row][next_col - 1] = '-'
  57.  
  58.         if matrix[next_row][next_col + 1] not in ['-', 'C'] and presents:
  59.             presents -= 1
  60.             if matrix[next_row][next_col + 1] == 'V':
  61.                 total_delivered_to_good_kids += 1
  62.             matrix[next_row][next_col + 1] = '-'
  63.  
  64.         if matrix[next_row - 1][next_col] not in ['-', 'C'] and presents:
  65.             presents -= 1
  66.             if matrix[next_row - 1][next_col] == 'V':
  67.                 total_delivered_to_good_kids += 1
  68.             matrix[next_row - 1][next_col] = '-'
  69.  
  70.         if matrix[next_row + 1][next_col] not in ['-', 'C'] and presents:
  71.             presents -= 1
  72.             if matrix[next_row + 1][next_col] == 'V':
  73.                 total_delivered_to_good_kids += 1
  74.             matrix[next_row + 1][next_col] = '-'
  75.  
  76.     santa_row, santa_col = next_row, next_col
  77.     matrix[santa_row][santa_col] = 'S'
  78.     if not presents:
  79.         break
  80.  
  81. if not presents and total_delivered_to_good_kids < total_good_kids:
  82.     print("Santa ran out of presents!")
  83.  
  84. for row in matrix:
  85.     print(*row)
  86.  
  87. if total_good_kids == total_delivered_to_good_kids:
  88.     print(f"Good job, Santa! {total_good_kids} happy nice kid/s.")
  89. else:
  90.     print(f"No presents for {total_good_kids - total_delivered_to_good_kids} nice kid/s.")
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement