Advent of Code 2022 https://adventofcode.com/2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day5-2.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from string import ascii_letters
  2. DEBUG=False
  3. STACKS_START = """FCPGQR
  4. WTCP
  5. BHPMC
  6. LTQSMPR
  7. PHJZVGN
  8. DPJ
  9. LGPZFJTR
  10. NLHCFPTJ
  11. GVZQHTCW"""
  12. def debug(*args):
  13. if DEBUG:
  14. print(args)
  15. def main():
  16. stacks = {}
  17. stacks_lines = STACKS_START.split("\n")
  18. for i, line in enumerate(stacks_lines):
  19. j = i+1
  20. for l in line:
  21. p = stacks.get(j, [])
  22. p.append(l)
  23. stacks[j] = p
  24. print(stacks)
  25. with open("input5.txt", "r") as file:
  26. inlines = [line.strip() for line in file.readlines()]
  27. instructions = []
  28. for line in inlines:
  29. words = line.split(" ")
  30. moves = []
  31. for word in words:
  32. try:
  33. moves.append(int(word))
  34. except ValueError:
  35. pass
  36. instructions.append(moves)
  37. for move in instructions:
  38. number, stack_from, stack_to = move
  39. n_num = 0-number
  40. f = stacks[stack_from]
  41. t = stacks[stack_to]
  42. f_moved = f[n_num:]
  43. f_left = f[:n_num]
  44. t.extend(f_moved)
  45. stacks[stack_from] = f_left
  46. stacks[stack_to] = t
  47. for num, stack in stacks.items():
  48. print(f"{num}: {stack[-1]}")
  49. if __name__ == "__main__":
  50. main()