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-1.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. f = stacks[stack_from]
  40. t = stacks[stack_to]
  41. for _ in range(number):
  42. crate = f.pop()
  43. t.append(crate)
  44. stacks[stack_from] = f
  45. stacks[stack_to] = t
  46. for num, stack in stacks.items():
  47. print(f"{num}: {stack[-1]}")
  48. if __name__ == "__main__":
  49. main()