Advertisement
Guest User

AoC 2022 D5

a guest
Dec 5th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | Source Code | 0 0
  1. from collections import deque
  2.  
  3.  
  4. INPUT = "input.txt"
  5.  
  6.  
  7. ### Input Processing ###
  8.  
  9. def build_contents_from_diagram(diagram):
  10.     length = len(diagram[0])
  11.     contents = []
  12.     index = 1
  13.  
  14.     while index <= length:
  15.         stacked = ""
  16.         for layer in diagram[::-1]:
  17.             c = layer[index]
  18.             if c.isalpha():
  19.                 stacked += c
  20.         contents.append(stacked)
  21.         index += 4
  22.  
  23.     return contents
  24.  
  25.  
  26. def build_steps(lines):
  27.     steps = []
  28.     for line in lines:
  29.         quantity, source, destination = [int(i) for i in line.split() if i.isnumeric()]
  30.         steps.append((quantity, source - 1, destination - 1))
  31.     return steps
  32.  
  33.  
  34. def build_stacks(diagram):
  35.     stacks = [deque(c) for c in build_contents_from_diagram(diagram)]
  36.     return stacks
  37.  
  38.  
  39. def process_input(text):
  40.     header, body = [section.split("\n") for section in text.split("\n\n")]
  41.     formatted_header = header[:-1]
  42.     return formatted_header, body
  43.  
  44.  
  45. def get_stacks_and_steps(path_in):
  46.     with open(path_in, "r") as file:
  47.         diagram, instructions = process_input(file.read())
  48.         stacks = build_stacks(diagram)
  49.         steps = build_steps(instructions)
  50.         return stacks, steps
  51.  
  52.  
  53. ### Part One ###
  54.  
  55. def move_unordered(source, destination, quantity):
  56.     for _ in range(quantity):
  57.         item = source.pop()
  58.         destination.append(item)
  59.  
  60.  
  61. ### Part Two ###
  62.  
  63. def move_ordered(source, destination, quantity):
  64.     order = []
  65.     for _ in range(quantity):
  66.         order += source.pop()
  67.  
  68.     for item in order[::-1]:
  69.         destination.append(item)
  70.  
  71.  
  72. ### Shared Functions ###
  73.  
  74. def get_top_of_stacks(stacks):
  75.     output = ""
  76.     for stack in stacks:
  77.  
  78.         try:
  79.             char = stack.pop()
  80.             output += char
  81.         except IndexError:
  82.             continue
  83.  
  84.     return output
  85.  
  86.  
  87. def shift_items(steps, stacks, ordered=False):
  88.     move = move_ordered if ordered else move_unordered
  89.     for step in steps:
  90.         quantity, source, destination = step
  91.         move(stacks[source], stacks[destination], quantity)
  92.  
  93.  
  94. ### Execution ###
  95.  
  96. if __name__ == "__main__":
  97.     crates, procedure = get_stacks_and_steps(INPUT)
  98.     shift_items(procedure, crates, ordered=True)  # Change "ordered" argument for each part.
  99.     print(get_top_of_stacks(crates))
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement