lichev96

02. North Pole Challenge

Oct 16th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. def count_items(matrix):
  2.     items = 0
  3.  
  4.     for x in matrix:
  5.         for c in x:
  6.             if c != '.' and c != 'Y' and c != 'x':
  7.                 items += 1
  8.  
  9.     return items
  10.  
  11.  
  12. def santa_position(matrix, rows, cols):
  13.     position = []
  14.     for r in range(rows):
  15.         for c in range(cols):
  16.             if matrix[r][c] == 'Y':
  17.                 position.append(r)
  18.                 position.append(c)
  19.     return position
  20.  
  21.  
  22. def walk(matrix, direction, steps, rows, cols):
  23.     santa_row, santa_col = santa_position(matrix, rows, cols)
  24.     lst_with_items = []
  25.  
  26.     if direction == 'right':
  27.         for x in range(int(steps)):
  28.             santa_col += 1
  29.             if santa_col > cols - 1:
  30.                 santa_col = 0
  31.             lst_with_items.append([santa_row, santa_col])
  32.  
  33.     elif direction == 'left':
  34.         for x in range(int(steps)):
  35.             santa_col -= 1
  36.             if santa_col < 0:
  37.                 santa_col = cols - 1
  38.             lst_with_items.append([santa_row, santa_col])
  39.     elif direction == 'up':
  40.         for x in range(int(steps)):
  41.             santa_row -= 1
  42.             if santa_row < 0:
  43.                 santa_row = rows - 1
  44.             lst_with_items.append([santa_row, santa_col])
  45.     elif direction == 'down':
  46.         for x in range(int(steps)):
  47.             santa_row += 1
  48.             if santa_row > rows - 1:
  49.                 santa_row = 0
  50.             lst_with_items.append([santa_row, santa_col])
  51.  
  52.     return lst_with_items
  53.  
  54.  
  55. rows, cols = map(int,input().split(", "))
  56.  
  57. matrix = []
  58. for r in range(rows):
  59.     current_row = input().split()
  60.     matrix.append(current_row)
  61.  
  62. dct = {
  63.     "Christmas decorations": 0,
  64.     "Gifts": 0,
  65.     'Cookies': 0
  66. }
  67.  
  68. no_items = False
  69. while True:
  70.     command = input()
  71.  
  72.     if command == 'End':
  73.         break
  74.  
  75.     directions, steps = command.split("-")
  76.     positions_of_items = walk(matrix, directions, steps, rows, cols)
  77.     santa_row, santa_col = santa_position(matrix, rows, cols)
  78.     matrix[santa_row][santa_col] = 'x'
  79.  
  80.     for x in range(len(positions_of_items)):
  81.         next_row, next_col = positions_of_items[x]
  82.         if matrix[next_row][next_col] == 'D':
  83.             dct['Christmas decorations'] += 1
  84.         elif matrix[next_row][next_col] == 'G':
  85.             dct['Gifts'] += 1
  86.         elif matrix[next_row][next_col] == 'C':
  87.             dct['Cookies'] += 1
  88.  
  89.         if x < len(positions_of_items) - 1:
  90.             matrix[next_row][next_col] = 'x'
  91.  
  92.         else:
  93.             matrix[next_row][next_col] = 'Y'
  94.  
  95.         items = count_items(matrix)
  96.         if items == 0:
  97.             matrix[next_row][next_col] = 'Y'
  98.             no_items = True
  99.             break
  100.     if no_items:
  101.         break
  102.     #
  103.     # [print(' '.join(x))for x in matrix]
  104.     # print()
  105.  
  106. if no_items:
  107.     print(f"Merry Christmas!")
  108. print(f"You've collected:")
  109. [print(f'- {value} {key}') for key, value in dct.items()]
  110. [print(' '.join(x))for x in matrix]
Advertisement
Add Comment
Please, Sign In to add comment