Advertisement
go6odn28

02_north_pole_challenge

Jun 9th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. def is_valid_index(idx, size):
  2.     return 0 <= idx < size
  3.  
  4.  
  5. def check_for_items_to_collect(symbol, collected_decorations):
  6.     if symbol in collected_decorations:
  7.         collected_decorations[symbol] += 1
  8.     return collected_decorations
  9.  
  10.  
  11. def check_if_all_items_are_collected(collected_decorations, cookies, decorations, gifts):
  12.     all_collected = collected_decorations['C'] == cookies and collected_decorations[
  13.         'D'] == decorations and collected_decorations['G'] == gifts
  14.     return all_collected
  15.  
  16.  
  17. def take_positions_and_data(field, my_row, my_col, cookies, gifts, decorations, rows, cols):
  18.     for r in range(rows):
  19.         row = input().split()
  20.         field.append(row)
  21.         if 'Y' in row:
  22.             my_row = r
  23.             my_col = row.index('Y')
  24.         if 'D' in row or 'C' or 'G' in row:
  25.             cookies += row.count('C')
  26.             gifts += row.count('G')
  27.             decorations += row.count('D')
  28.  
  29.     return field, my_row, my_col, cookies, gifts, decorations
  30.  
  31.  
  32. def validate_directions(next_row, next_col, rows, cols, direction):
  33.     revert_moves = {'up': next_row + rows,
  34.                     'down': next_row - rows,
  35.                     'left': next_col + cols,
  36.                     'right': next_col - cols
  37.                     }
  38.  
  39.     if not is_valid_index(next_row, rows):
  40.         next_row = revert_moves[direction]
  41.  
  42.     elif not is_valid_index(next_col, cols):
  43.         next_col = revert_moves[direction]
  44.  
  45.     return next_row, next_col
  46.  
  47.  
  48. def next_move(field, my_row, my_col, steps, direction, rows, cols, collected_decorations,
  49.               all_collected_items, cookies, gifts, decorations):
  50.     possible_moves = {'up': [-1, 0],
  51.                       'down': [1, 0],
  52.                       'left': [0, -1],
  53.                       'right': [0, 1]
  54.                       }
  55.  
  56.     past_moves = []
  57.  
  58.     for step in range(steps):
  59.         next_row, next_col = my_row + possible_moves[direction][0], my_col + possible_moves[direction][1]
  60.  
  61.         next_row, next_col = validate_directions(next_row, next_col, rows, cols, direction)
  62.         my_row, my_col = next_row, next_col
  63.         moves = (next_row, next_col)
  64.         if moves not in past_moves:
  65.             past_moves.append(moves)
  66.  
  67.     for move in past_moves:
  68.         r, c = int(move[0]), int(move[1])
  69.         symbol = field[r][c]
  70.         collected_decorations = check_for_items_to_collect(symbol, collected_decorations)
  71.         all_collected_items = check_if_all_items_are_collected(collected_decorations, cookies, decorations, gifts)
  72.  
  73.         if all_collected_items:
  74.             field[r][c] = "x"
  75.             field[r][c] = "Y"
  76.             break
  77.         field[r][c] = "Y"  # just for temporary check the position
  78.         field[r][c] = "x"
  79.  
  80.     return field, my_row, my_col, collected_decorations, all_collected_items, cookies, gifts, decorations
  81.  
  82.  
  83. def print_result(field, collected_decorations, all_collected_items):
  84.     if all_collected_items:
  85.         print("Merry Christmas!")
  86.  
  87.     print("You've collected:\n"
  88.           f"- {collected_decorations['D']} Christmas decorations\n"
  89.           f"- {collected_decorations['G']} Gifts\n"
  90.           f"- {collected_decorations['C']} Cookies")
  91.  
  92.     for r in field:
  93.         print(' '.join(r))
  94.  
  95.  
  96. def main():
  97.     cookies = 0
  98.     gifts = 0
  99.     decorations = 0
  100.  
  101.     all_collected_items = False
  102.     collected_decorations = {'C': 0,
  103.                              'G': 0,
  104.                              'D': 0}
  105.     field = []
  106.     my_row, my_col = 0, 0
  107.     rows, cols = list(map(int, input().split(', ')))
  108.  
  109.     field, my_row, my_col, cookies, gifts, decorations = (
  110.         take_positions_and_data(field, my_row, my_col, cookies, gifts, decorations, rows, cols))
  111.  
  112.     start_row, start_col = my_row, my_col
  113.  
  114.     while True:
  115.         if all_collected_items:
  116.             field[start_row][start_col] = 'x'
  117.             break
  118.  
  119.         command = input()
  120.         if command == 'End':
  121.             field[start_row][start_col] = 'x'
  122.             field[my_row][my_col] = 'Y'
  123.             break
  124.  
  125.         direction, steps = command.split('-')
  126.         steps = int(steps)
  127.  
  128.         field, my_row, my_col, collected_decorations, all_collected_items, cookies, gifts, decorations = (
  129.             next_move(field, my_row, my_col, steps, direction, rows, cols, collected_decorations, all_collected_items,
  130.                       cookies, gifts, decorations))
  131.  
  132.         all_collected_items = check_if_all_items_are_collected(collected_decorations, cookies, decorations, gifts)
  133.  
  134.     print_result(field, collected_decorations, all_collected_items)
  135.  
  136.  
  137. if __name__ == '__main__':
  138.     main()
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement