Advertisement
Nenogzar

02. North Pole Challenge

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