Advertisement
desrtfx

Day_05.py

Dec 5th, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import re
  2. import copy
  3.  
  4. def get_result(stacks):
  5.     result = []
  6.     for s in stacks:
  7.         c = s.pop()
  8.         s.append(c)
  9.         result.append(c)
  10.     result = "".join(result)
  11.     return result
  12.  
  13.  
  14. def part01(stacks, cmds):
  15.     s1 = copy.deepcopy(stacks)
  16.     for cmd in cmds:
  17.         how_many = cmd[0]
  18.         from_stack = cmd[1]
  19.         to_stack = cmd[2]
  20.         for i in range(how_many):
  21.             c = s1[from_stack].pop()
  22.             s1[to_stack].append(c)
  23.     return get_result(s1)
  24.  
  25.  
  26. def part02(stacks, cmds):
  27.     s1 = copy.deepcopy(stacks)
  28.     for cmd in cmds:
  29.         how_many = cmd[0]
  30.         from_stack = cmd[1]
  31.         to_stack = cmd[2]
  32.         c = []
  33.         for i in range(how_many):
  34.             c.append(s1[from_stack].pop())
  35.         c.reverse()
  36.         for d in c:
  37.             s1[to_stack].append(d)
  38.     return get_result(s1)
  39.  
  40. stacks = []
  41. stacks.append(['G','F','V','H','P','S'])
  42. stacks.append(['G','J','F','B','V','D','Z','M'])
  43. stacks.append(['G','M','L','J','N'])
  44. stacks.append(['N','G','Z','V','D','W','P'])
  45. stacks.append(['V','R','C','B'])
  46. stacks.append(['V','R','S','M','P','W','L','Z'])
  47. stacks.append(['T','H','P'])
  48. stacks.append(['Q','R','S','N','C','H','Z','V'])
  49. stacks.append(['F','L','G','P','V','Q','J'])
  50.  
  51. with open("Input_Day05.txt") as f:
  52.     raw_data = [x.strip() for x in f]
  53.  
  54. cmds = []
  55. for line in raw_data:
  56.     num = re.findall(r'\d+', line)
  57.     how_many = int(num[0])
  58.     from_stack = int(num[1]) - 1
  59.     to_stack = int(num[2]) - 1
  60.     cmds.append([how_many, from_stack, to_stack])
  61.  
  62.  
  63. result = part01(stacks, cmds)
  64. print(f"Part 01: {result}")
  65.  
  66. result = part02(stacks, cmds)
  67. print(f"Part 02: {result}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement