Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import copy
- def get_result(stacks):
- result = []
- for s in stacks:
- c = s.pop()
- s.append(c)
- result.append(c)
- result = "".join(result)
- return result
- def part01(stacks, cmds):
- s1 = copy.deepcopy(stacks)
- for cmd in cmds:
- how_many = cmd[0]
- from_stack = cmd[1]
- to_stack = cmd[2]
- for i in range(how_many):
- c = s1[from_stack].pop()
- s1[to_stack].append(c)
- return get_result(s1)
- def part02(stacks, cmds):
- s1 = copy.deepcopy(stacks)
- for cmd in cmds:
- how_many = cmd[0]
- from_stack = cmd[1]
- to_stack = cmd[2]
- c = []
- for i in range(how_many):
- c.append(s1[from_stack].pop())
- c.reverse()
- for d in c:
- s1[to_stack].append(d)
- return get_result(s1)
- stacks = []
- stacks.append(['G','F','V','H','P','S'])
- stacks.append(['G','J','F','B','V','D','Z','M'])
- stacks.append(['G','M','L','J','N'])
- stacks.append(['N','G','Z','V','D','W','P'])
- stacks.append(['V','R','C','B'])
- stacks.append(['V','R','S','M','P','W','L','Z'])
- stacks.append(['T','H','P'])
- stacks.append(['Q','R','S','N','C','H','Z','V'])
- stacks.append(['F','L','G','P','V','Q','J'])
- with open("Input_Day05.txt") as f:
- raw_data = [x.strip() for x in f]
- cmds = []
- for line in raw_data:
- num = re.findall(r'\d+', line)
- how_many = int(num[0])
- from_stack = int(num[1]) - 1
- to_stack = int(num[2]) - 1
- cmds.append([how_many, from_stack, to_stack])
- result = part01(stacks, cmds)
- print(f"Part 01: {result}")
- result = part02(stacks, cmds)
- print(f"Part 02: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement