Advertisement
GalinaKG

Advent of code - Day 5 - First task

Dec 5th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 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.  
  24. while count_to_move:
  25. if count_to_move > 0 and not stack:
  26. count_to_move = 0
  27. break
  28.  
  29. stack_where_to_move.insert(0, (stack.pop(0)))
  30. count_to_move -= 1
  31.  
  32.  
  33. result = f'{stack1[0]}{stack2[0]}{stack3[0]}{stack4[0]}{stack5[0]}{stack6[0]}{stack7[0]}{stack8[0]}{stack9[0]}'
  34.  
  35. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement