Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vdef validate_coordinate(item, limit):
- return item if 0 <= item < limit else (limit - 1 if item < 0 else 0)
- def move_y(start_row, start_col, direction, steps, workshop, rows, cols, current_position, empty_position,
- last_position, collected_items):
- for _ in range(steps):
- new_row, new_col = start_row, start_col
- if direction == "up":
- new_row -= 1
- elif direction == "down":
- new_row += 1
- elif direction == "left":
- new_col -= 1
- elif direction == "right":
- new_col += 1
- new_row = validate_coordinate(new_row, rows)
- new_col = validate_coordinate(new_col, cols)
- new_cell = workshop[new_row][new_col]
- if new_cell in collected_items:
- collected_items[new_cell][1] += 1
- workshop[new_row][new_col] = empty_position
- if (new_row, new_col) != (start_row, start_col):
- workshop[start_row][start_col] = last_position
- workshop[new_row][new_col] = current_position
- start_row, start_col = new_row, new_col
- all_items_collected = all(collected_items[key][0] == collected_items[key][1] for key in collected_items)
- if all_items_collected:
- break
- return start_row, start_col, all_items_collected
- current_position, empty_position, last_position = "Y", ".", "x"
- workshop = []
- collected_items = {"D": [0, 0], "G": [0, 0], "C": [0, 0]}
- rows, cols = map(int, input().split(", "))
- for row in range(rows):
- line = input().split()
- workshop.append(line)
- for col, cell in enumerate(line):
- if cell in collected_items:
- collected_items[cell][0] += 1
- start_row, start_col = -1, -1
- for row in range(rows):
- for col in range(cols):
- if workshop[row][col] == current_position:
- start_row, start_col = row, col
- break
- if start_row != -1:
- break
- while True:
- distination = input().strip()
- if distination.lower() == 'end':
- break
- direction, steps = distination.split('-')
- steps = int(steps)
- start_row, start_col, all_items_collected = move_y(start_row, start_col, direction, steps, workshop, rows, cols,
- current_position, empty_position, last_position, collected_items)
- if all_items_collected:
- break
- if all_items_collected:
- print("\nMerry Christmas!")
- print("You've collected:")
- print(f"- {collected_items['D'][1]} Christmas decorations")
- print(f"- {collected_items['G'][1]} Gifts")
- print(f"- {collected_items['C'][1]} Cookies")
- for line in workshop:
- print(" ".join(line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement