Yanam

seashell_treasure

Feb 14th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. n = int(input())
  2. matrix = [[x for x in input().split()] for _ in range(n)]
  3. # print(matrix)
  4. collection = []
  5. stolen = 0
  6.  
  7.  
  8. def collect(m, r, c, l):
  9.     if 0 <= r < len(m) and 0 <= c < len(m[r]) and m[r][c] != '-':
  10.         l.append(m[r][c])
  11.         matrix[r][c] = '-'
  12.  
  13.  
  14. def steal(d, r, c, m):
  15.     s = 0
  16.     if 0 <= r < len(m) and 0 <= c < len(matrix[r]) and m[r][c] != '-':
  17.         s += 1
  18.         m[r][c] = '-'
  19.         for i in range(3):
  20.             if d == 'up' and 0 <= r-1 < len(m) and 0 <= c < len(m[r-1]):
  21.                 r -= 1
  22.                 if m[r][c] != '-':
  23.                     m[r][c] = '-'
  24.                     s += 1
  25.             elif d == 'down' and 0 <= r+1 < len(m) and 0 <= c < len(m[r+1]):
  26.                 r += 1
  27.                 if m[r][c] != '-':
  28.                     m[r][c] = '-'
  29.                     s += 1
  30.             elif d == 'left' and 0 <= c-1 < len(m[r]):
  31.                 c -= 1
  32.                 if m[r][c] != '-':
  33.                     m[r][c] = '-'
  34.                     s += 1
  35.             elif d == 'right' and 0 <= c+1 < len(m[r]):
  36.                 c += 1
  37.                 if m[r][c] != '-':
  38.                     m[r][c] = '-'
  39.                     s += 1
  40.             else:
  41.                 return s
  42.     return s
  43.  
  44.  
  45. while True:
  46.     data = input()
  47.     if data == 'Sunset':
  48.         break
  49.     command = data.split()
  50.     if command[0] == 'Collect':
  51.         collect(matrix, int(command[1]), int(command[2]), collection)
  52.     elif command[0] == 'Steal':
  53.         stolen += steal(command[3], int(command[1]), int(command[2]), matrix)
  54.  
  55.  
  56. for line in matrix:
  57.     print(' '.join(line))
  58. if collection:
  59.     print(f'Collected seashells: {len(collection)} -> {", ".join(collection)}')
  60. else:
  61.     print('Collected seashells: 0')
  62. print(f"Stolen seashells: {stolen}")
Advertisement
Add Comment
Please, Sign In to add comment