Advertisement
Nenogzar

02. North Pole Challenge

Jun 13th, 2024
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. vdef validate_coordinate(item, limit):
  2.     return item if 0 <= item < limit else (limit - 1 if item < 0 else 0)
  3.  
  4. def move_y(start_row, start_col, direction, steps, workshop, rows, cols, current_position, empty_position,
  5.            last_position, collected_items):
  6.  
  7.     for _ in range(steps):
  8.         new_row, new_col = start_row, start_col
  9.  
  10.         if direction == "up":
  11.             new_row -= 1
  12.         elif direction == "down":
  13.             new_row += 1
  14.         elif direction == "left":
  15.             new_col -= 1
  16.         elif direction == "right":
  17.             new_col += 1
  18.  
  19.         new_row = validate_coordinate(new_row, rows)
  20.         new_col = validate_coordinate(new_col, cols)
  21.  
  22.         new_cell = workshop[new_row][new_col]
  23.         if new_cell in collected_items:
  24.             collected_items[new_cell][1] += 1
  25.             workshop[new_row][new_col] = empty_position
  26.  
  27.         if (new_row, new_col) != (start_row, start_col):
  28.             workshop[start_row][start_col] = last_position
  29.  
  30.         workshop[new_row][new_col] = current_position
  31.         start_row, start_col = new_row, new_col
  32.  
  33.         all_items_collected = all(collected_items[key][0] == collected_items[key][1] for key in collected_items)
  34.         if all_items_collected:
  35.             break
  36.  
  37.     return start_row, start_col, all_items_collected
  38.  
  39.  
  40. current_position, empty_position, last_position = "Y", ".", "x"
  41. workshop = []
  42. collected_items = {"D": [0, 0], "G": [0, 0], "C": [0, 0]}
  43.  
  44. rows, cols = map(int, input().split(", "))
  45.  
  46. for row in range(rows):
  47.     line = input().split()
  48.     workshop.append(line)
  49.  
  50.     for col, cell in enumerate(line):
  51.         if cell in collected_items:
  52.             collected_items[cell][0] += 1
  53.  
  54. start_row, start_col = -1, -1
  55. for row in range(rows):
  56.     for col in range(cols):
  57.         if workshop[row][col] == current_position:
  58.             start_row, start_col = row, col
  59.             break
  60.     if start_row != -1:
  61.         break
  62.  
  63. while True:
  64.     distination = input().strip()
  65.     if distination.lower() == 'end':
  66.         break
  67.  
  68.     direction, steps = distination.split('-')
  69.     steps = int(steps)
  70.  
  71.     start_row, start_col, all_items_collected = move_y(start_row, start_col, direction, steps, workshop, rows, cols,
  72.                                                        current_position, empty_position, last_position, collected_items)
  73.  
  74.     if all_items_collected:
  75.         break
  76.  
  77. if all_items_collected:
  78.     print("\nMerry Christmas!")
  79.  
  80. print("You've collected:")
  81. print(f"- {collected_items['D'][1]} Christmas decorations")
  82. print(f"- {collected_items['G'][1]} Gifts")
  83. print(f"- {collected_items['C'][1]} Cookies")
  84.  
  85. for line in workshop:
  86.     print(" ".join(line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement