You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
import aoclib |
|
|
|
from collections import deque |
|
|
|
|
|
inp = aoclib.get_input(5, parser=aoclib.parse_lines) |
|
|
|
|
|
def parse_more(lines): |
|
stackinfo = [] |
|
instructions = [] |
|
flip = False |
|
|
|
for line in lines: |
|
if line == "": |
|
flip = True |
|
continue |
|
elif not flip: |
|
stackinfo.append(line) |
|
else: |
|
instructions.append(line) |
|
|
|
stacklist = stackinfo.pop(-1) |
|
haha_wtf = zip(*reversed(stackinfo)) |
|
stacks = {} |
|
highest_stack_num = 0 |
|
|
|
for index, stack in zip(stacklist, haha_wtf): |
|
if index != " ": |
|
highest_stack_num = int(index) |
|
stacks[int(index)] = deque([x for x in stack if x != " "]) |
|
|
|
parsed_instructions = [] |
|
|
|
for line in instructions: |
|
line = line[5:] # remove the "move " at the start |
|
count, rest = line.split(" from ") |
|
count = int(count) |
|
src, dest = rest.split(" to ") |
|
src = int(src) |
|
dest = int(dest) |
|
parsed_instructions.append((count, src, dest)) |
|
|
|
return stacks, parsed_instructions, highest_stack_num # ??? what the fuck just happened |
|
|
|
|
|
def part1(): |
|
stacks, instructions, stacknum = parse_more(inp) |
|
|
|
for count, src, dest in instructions: |
|
for i in range(count): |
|
stacks[dest].append(stacks[src].pop()) |
|
|
|
output = "" |
|
for i in range(1, stacknum+1): |
|
output += stacks[i].pop() |
|
|
|
return output |
|
|
|
|
|
def part2(): |
|
stacks, instructions, stacknum = parse_more(inp) |
|
|
|
for count, src, dest in instructions: |
|
buf = [] |
|
for i in range(count): |
|
buf.append(stacks[src].pop()) |
|
|
|
for crate in reversed(buf): |
|
stacks[dest].append(crate) |
|
|
|
output = "" |
|
for i in range(1, stacknum+1): |
|
output += stacks[i].pop() |
|
|
|
return output |
|
|
|
|
|
aoclib.main(part1, part2)
|
|
|