Advertisement
GalinaKG

Advent of code - Day 5 - Second task

Dec 5th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. stack1 = ['F', 'R', 'W']
  2. stack2 = ['P', 'W', 'V', 'D', 'C', 'M', 'H', 'T']
  3. stack3 = ['L', 'N', 'Z', 'M', 'P']
  4. stack4 = ['R', 'H', 'C', 'J']
  5. stack5 = ['B', 'T', 'Q', 'H', 'G', 'P', 'C']
  6. stack6 = ['Z', 'F', 'L', 'W', 'C', 'G']
  7. stack7 = ['C', 'G', 'J', 'Z', 'Q', 'L', 'V', 'W']
  8. stack8 = ['C', 'V', 'T', 'W', 'F', 'R', 'N', 'P']
  9. stack9 = ['V', 'S', 'R', 'G', 'H', 'W', 'J']
  10. list_of_stacks = [stack1, stack2, stack3, stack4, stack5, stack6, stack7, stack8, stack9]
  11. with open("text.txt", "r") as file:
  12.     for line in file:
  13.         line = line.strip('\n').split()
  14.         if line and line[0] == 'move':
  15.             count_to_move = int(line[1])
  16.             move_from = int(line[3])
  17.             move_to = int(line[5])
  18.         else:
  19.             continue
  20.  
  21.         stack = list_of_stacks[move_from - 1]
  22.         stack_where_to_move = list_of_stacks[move_to - 1]
  23.         curr_elements = []
  24.  
  25.         while count_to_move:
  26.             if count_to_move > 0 and not stack:
  27.                 count_to_move = 0
  28.                 break
  29.  
  30.             curr_elements.insert(0, stack.pop(0))
  31.             count_to_move -= 1
  32.  
  33.         for el in curr_elements:
  34.             stack_where_to_move.insert(0, el)
  35.  
  36. result = f'{stack1[0]}{stack2[0]}{stack3[0]}{stack4[0]}{stack5[0]}{stack6[0]}{stack7[0]}{stack8[0]}{stack9[0]}'
  37.  
  38. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement