Created
December 12, 2022 12:09
-
-
Save rtt/6b5728ca1399e6b6bbb011ebd7a18f05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def partN(input_data, one_at_a_time): | |
step = 4 | |
instructions = [] | |
stack_data, instruction_data = input_data.split('\n\n') | |
cols = int(stack_data.strip().split()[-1]) # ugh? | |
stacks = [''] * cols | |
stacks_data_lines = list(reversed(stack_data.split('\n')[:-1])) | |
col_indexes = range(1, cols * step, step) # range(1, 10, 4) | |
for line in stacks_data_lines: | |
for ix, col in enumerate(col_indexes): | |
try: | |
char = line[col] | |
except: | |
pass | |
else: | |
if char.strip(): | |
stacks[ix] += char | |
for instruction in instruction_data.split('\n'): | |
# ugh? | |
num = int(instruction.split()[1]) | |
from_ = int(instruction.split()[3])-1 | |
to = int(instruction.split()[5])-1 | |
if one_at_a_time: | |
stacks[to] += ''.join(reversed(stacks[from_][-num:])) | |
else: | |
stacks[to] += stacks[from_][-num:] | |
stacks[from_] = stacks[from_][:-num] | |
return ''.join([s[-1] for s in stacks]) | |
if __name__ == '__main__': | |
with open('./input.txt') as f: | |
inp = f.read() | |
print(partN(inp, True)) | |
print(partN(inp, False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment