1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from string import ascii_letters
-
- DEBUG=False
-
- STACKS_START = """FCPGQR
- WTCP
- BHPMC
- LTQSMPR
- PHJZVGN
- DPJ
- LGPZFJTR
- NLHCFPTJ
- GVZQHTCW"""
-
- def debug(*args):
- if DEBUG:
- print(args)
-
- def main():
- stacks = {}
- stacks_lines = STACKS_START.split("\n")
- for i, line in enumerate(stacks_lines):
- j = i+1
- for l in line:
- p = stacks.get(j, [])
- p.append(l)
- stacks[j] = p
-
- print(stacks)
- with open("input5.txt", "r") as file:
- inlines = [line.strip() for line in file.readlines()]
-
- instructions = []
- for line in inlines:
- words = line.split(" ")
- moves = []
- for word in words:
- try:
- moves.append(int(word))
- except ValueError:
- pass
- instructions.append(moves)
-
- for move in instructions:
- number, stack_from, stack_to = move
- f = stacks[stack_from]
- t = stacks[stack_to]
- for _ in range(number):
- crate = f.pop()
- t.append(crate)
- stacks[stack_from] = f
- stacks[stack_to] = t
-
- for num, stack in stacks.items():
- print(f"{num}: {stack[-1]}")
-
-
- if __name__ == "__main__":
- main()
|