Advertisement
simeonshopov

The Garden

Jun 25th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. VEGETABLES = ('L', 'P', 'C')
  2. harvested = {k: 0 for k in VEGETABLES}
  3. harmed = 0
  4. DIRECTIONS = {
  5.     'up': (-2, 0),
  6.     'right': (0, +2),
  7.     'down': (+2, 0),
  8.     'left': (0, -2),
  9. }
  10.  
  11.  
  12. def next_position(r, c, cc):
  13.     global DIRECTIONS
  14.  
  15.     r = r + DIRECTIONS[cc][0]
  16.     c = c + DIRECTIONS[cc][1]
  17.  
  18.     return r, c
  19.  
  20.  
  21. def collect_harvest(c):
  22.     global harvested
  23.  
  24.     harvested[c] += 1
  25.  
  26.  
  27. def is_valid(n, rng):
  28.     return 0 <= n < rng
  29.  
  30.  
  31. def harvest(r, c):
  32.     global garden, VEGETABLES
  33.  
  34.     if is_valid(r, rows_count) and is_valid(c, len(garden[r])):
  35.         cell = garden[r][c]
  36.         if cell in VEGETABLES:
  37.             collect_harvest(cell)
  38.             garden[r][c] = ' '
  39.  
  40.  
  41. def move_mole(r: int, c: int, d: str):
  42.      global harmed, garden
  43.  
  44.      while True:
  45.          if not is_valid(r, rows_count) or not is_valid(c, len(garden[r])):
  46.             break
  47.          cell = garden[r][c]
  48.          if cell in VEGETABLES:
  49.              harmed += 1
  50.              garden[r][c] = ' '
  51.          r, c = next_position(r, c, d)
  52.  
  53.  
  54. def output():
  55.     global garden, harvested, harmed
  56.  
  57.     garden_output = "\n".join([' '.join(x) for x in garden])
  58.     harvested_output = f"Carrots: {harvested['C']}\nPotatos: {harvested['P']}\nLettuce: {harvested['L']}"
  59.     harmed_output = f'Harmed vegetables: {harmed}'
  60.  
  61.     return f'{garden_output}\n{harvested_output}\n{harmed_output}'
  62.  
  63. rows_count = int(input())
  64. garden = [input().split() for _ in range(rows_count)]
  65.  
  66.  
  67. """Нямам проверка за размера на матрицата дали изобщо ще съществува понеже в условието не са посочени
  68. ограничения. Но типично кода ще работи ако входа е коректен и rows_count е по-голямо от 2."""
  69.  
  70. while True:
  71.     command = input()
  72.     if command == 'End of Harvest':
  73.         break
  74.     tokens = command.split()
  75.     if tokens[0] == 'Harvest':
  76.         row, col = int(tokens[1]), int(tokens[2])
  77.         harvest(row, col)
  78.     elif tokens[0] == 'Mole':
  79.         row, col, direction = int(tokens[1]), int(tokens[2]), tokens[3]
  80.         move_mole(row, col, direction)
  81.  
  82. print(output())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement