Kaloyankerr

02. Present Delivery

Oct 19th, 2020 (edited)
2,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. def get_good_kids():
  2.     global matrix
  3.     res = 0
  4.     for x in matrix:
  5.         if 'V' in x:
  6.             res += x.count('V')
  7.     return res
  8.  
  9.  
  10. def get_start_position():
  11.     global matrix, n
  12.     for x in range(n):
  13.         if 'S' in matrix[x]:
  14.             return [x, matrix[x].index('S')]
  15.  
  16.  
  17. def santa_got_a_cookie():
  18.     global matrix, row, col, count_of_presents, good_kids_counter
  19.     for r in range(-1, 2):
  20.         for c in range(-1, 2):
  21.             if 0 <= (next_row + r) < n and 0 <= (next_col + c) < n:
  22.                 if matrix[next_row + r][next_col + c] != '-':
  23.                     count_of_presents -= 1
  24.                     if matrix[next_row + r][next_col + c] == 'V':
  25.                         good_kids_counter -= 1
  26.  
  27.                 matrix[next_row + r][next_col + c] = '-'
  28.  
  29.  
  30. count_of_presents = int(input())
  31. n = int(input())
  32.  
  33. matrix = [input().split() for x in range(n)]
  34.  
  35. santa_position = get_start_position()
  36. good_kids_counter = get_good_kids()
  37.  
  38. good_kids_counter_copy = good_kids_counter
  39.  
  40. moves = {
  41.     'up': [-1, 0],
  42.     'down': [1, 0],
  43.     'left': [0, -1],
  44.     'right': [0, 1]
  45. }
  46.  
  47. while count_of_presents > 0 and good_kids_counter > 0:
  48.     data = input()
  49.     if data == 'Christmas morning':
  50.         break
  51.  
  52.     row, col = santa_position
  53.     next_row, next_col = row + moves[data][0], col + moves[data][1]
  54.  
  55.     if 0 <= next_row < n and 0 <= next_col < n:
  56.         matrix[row][col] = '-'
  57.         matrix_position = matrix[next_row][next_col]
  58.  
  59.         santa_position = [next_row, next_col]
  60.  
  61.         if matrix_position == 'V':
  62.             count_of_presents -= 1
  63.             good_kids_counter -= 1
  64.  
  65.         elif matrix_position == 'C':
  66.             santa_got_a_cookie()
  67.  
  68.         matrix[next_row][next_col] = 'S'
  69.  
  70. if count_of_presents == 0 and good_kids_counter > 0:
  71.     print('Santa ran out of presents!')
  72.  
  73. [print(' '.join(x)) for x in matrix]
  74. if good_kids_counter == 0:
  75.     print(f'Good job, Santa! {good_kids_counter_copy} happy nice kid/s.')
  76. else:
  77.     print(f'No presents for {good_kids_counter} nice kid/s.')
Advertisement
Add Comment
Please, Sign In to add comment