Yanam

present_delivery

Feb 8th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. m = int(input())
  2. n = int(input())
  3. matrix = [[x for x in input().split()] for _ in range(n)]
  4.  
  5. nice_count = 0
  6.  
  7.  
  8. def count_nice(data):
  9.     nice = 0
  10.     for el in data:
  11.         nice += el.count('V')
  12.     return nice
  13.  
  14.  
  15. def moves(c, row, col):
  16.     global matrix
  17.     matrix[row][col] = '-'
  18.     if c == 'up' and row-1 >= 0:
  19.         position = [row-1, col]
  20.     elif c == 'down' and row+1 < len(matrix):
  21.         position = [row+1, col]
  22.     elif c == 'right' and col+1 < len(matrix):
  23.         position = [row, col+1]
  24.     elif c == 'left' and col-1 >= 0:
  25.         position = [row, col-1]
  26.     else:
  27.         position = [row, col]
  28.     return position[0], position[1]
  29.  
  30.  
  31. def actions(rw, cw):
  32.     global matrix
  33.     global nice_count
  34.     global m
  35.     global all_nice
  36.     if matrix[rw][cw] == 'V':
  37.         matrix[rw][cw] = '-'
  38.         nice_count += 1
  39.         m -= 1
  40.         if m == 0 and nice_count < all_nice:
  41.             return "Santa ran out of presents!"
  42.         elif nice_count == all_nice:
  43.             return 'Done'
  44.     elif matrix[rw][cw] == 'X':
  45.         matrix[rw][cw] = '-'
  46.     elif matrix[rw][cw] == 'C':
  47.         if (matrix[rw-1][cw] == 'V' or matrix[rw-1][cw] == 'X') and m > 0:
  48.             if matrix[rw-1][cw] == 'V':
  49.                 nice_count += 1
  50.             matrix[rw-1][cw] = '-'
  51.             m -= 1
  52.         if (matrix[rw+1][cw] == 'V' or matrix[rw+1][cw] == 'X') and m > 0:
  53.             if matrix[rw+1][cw] == 'V':
  54.                 nice_count += 1
  55.             matrix[rw+1][cw] = '-'
  56.             m -= 1
  57.         if (matrix[rw][cw-1] == 'V' or matrix[rw][cw-1] == 'X') and m > 0:
  58.             if matrix[rw][cw-1] == 'V':
  59.                 nice_count += 1
  60.             matrix[rw][cw-1] = '-'
  61.             m -= 1
  62.         if (matrix[rw][cw+1] == 'V' or matrix[rw][cw+1] == 'X') and m > 0:
  63.             if matrix[rw][cw+1] == 'V':
  64.                 nice_count += 1
  65.             matrix[rw][cw+1] = '-'
  66.             m -= 1
  67.         if m == 0 and nice_count < all_nice:
  68.             return "Santa ran out of presents!"
  69.     return
  70.  
  71.  
  72. all_nice = count_nice(matrix)
  73. start = []
  74. for i in range(len(matrix)):
  75.     for j in range(len(matrix)):
  76.         if matrix[i][j] == 'S':
  77.             start = [i, j]
  78.  
  79. while True:
  80.     command = input()
  81.     if command == 'Christmas morning':
  82.         break
  83.     start = moves(command, start[0], start[1])
  84.     message = actions(start[0], start[1])
  85.     matrix[start[0]][start[1]] = 'S'
  86.     if message:
  87.         break
  88.  
  89. if nice_count < all_nice and m == 0:
  90.     print('Santa ran out of presents!')
  91. for element in matrix:
  92.     print(' '.join(element))
  93.  
  94. if nice_count == all_nice:
  95.     print(f"Good job, Santa! {nice_count} happy nice kid/s.")
  96. else:
  97.     left = all_nice - nice_count
  98.     print(f'No presents for {left} nice kid/s.')
Add Comment
Please, Sign In to add comment