Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- VEGETABLES = ('L', 'P', 'C')
- harvested = {k: 0 for k in VEGETABLES}
- harmed = 0
- DIRECTIONS = {
- 'up': (-2, 0),
- 'right': (0, +2),
- 'down': (+2, 0),
- 'left': (0, -2),
- }
- def next_position(r, c, cc):
- global DIRECTIONS
- r = r + DIRECTIONS[cc][0]
- c = c + DIRECTIONS[cc][1]
- return r, c
- def collect_harvest(c):
- global harvested
- harvested[c] += 1
- def is_valid(n, rng):
- return 0 <= n < rng
- def harvest(r, c):
- global garden, VEGETABLES
- if is_valid(r, rows_count) and is_valid(c, len(garden[r])):
- cell = garden[r][c]
- if cell in VEGETABLES:
- collect_harvest(cell)
- garden[r][c] = ' '
- def move_mole(r: int, c: int, d: str):
- global harmed, garden
- while True:
- if not is_valid(r, rows_count) or not is_valid(c, len(garden[r])):
- break
- cell = garden[r][c]
- if cell in VEGETABLES:
- harmed += 1
- garden[r][c] = ' '
- r, c = next_position(r, c, d)
- def output():
- global garden, harvested, harmed
- garden_output = "\n".join([' '.join(x) for x in garden])
- harvested_output = f"Carrots: {harvested['C']}\nPotatos: {harvested['P']}\nLettuce: {harvested['L']}"
- harmed_output = f'Harmed vegetables: {harmed}'
- return f'{garden_output}\n{harvested_output}\n{harmed_output}'
- rows_count = int(input())
- garden = [input().split() for _ in range(rows_count)]
- """Нямам проверка за размера на матрицата дали изобщо ще съществува понеже в условието не са посочени
- ограничения. Но типично кода ще работи ако входа е коректен и rows_count е по-голямо от 2."""
- while True:
- command = input()
- if command == 'End of Harvest':
- break
- tokens = command.split()
- if tokens[0] == 'Harvest':
- row, col = int(tokens[1]), int(tokens[2])
- harvest(row, col)
- elif tokens[0] == 'Mole':
- row, col, direction = int(tokens[1]), int(tokens[2]), tokens[3]
- move_mole(row, col, direction)
- print(output())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement