83 lines
2.1 KiB
Python
83 lines
2.1 KiB
Python
|
from pprint import pprint as pp
|
||
|
from shared import load_rows
|
||
|
from scanf import scanf
|
||
|
from string import ascii_letters
|
||
|
import re
|
||
|
|
||
|
|
||
|
def load_crates(x):
|
||
|
done_crates = False
|
||
|
crates = []
|
||
|
instructions = []
|
||
|
for row in x:
|
||
|
if done_crates:
|
||
|
instructions.append(row)
|
||
|
else:
|
||
|
if row == '':
|
||
|
done_crates = True
|
||
|
else:
|
||
|
crates.append(row)
|
||
|
crates.pop()
|
||
|
return crates, instructions
|
||
|
|
||
|
def to_lists(crates):
|
||
|
parsed = []
|
||
|
reg = re.compile(r"[\([{})\]]")
|
||
|
for row in crates:
|
||
|
parsed.append([x for x in reg.sub(" ",row)])
|
||
|
parsed = list(zip(*parsed[::-1]))
|
||
|
parsed = list(zip(*parsed[::-1]))
|
||
|
parsed = list(zip(*parsed[::-1]))
|
||
|
cleaned1 = [[x for x in y if x.strip()] for y in parsed if y]
|
||
|
cleaned2 = [x for x in cleaned1 if x != []][::-1]
|
||
|
return cleaned2
|
||
|
|
||
|
def parse_instructions(crates, instructions):
|
||
|
for instruction in instructions:
|
||
|
count,_from,_to = scanf("move %d from %d to %d", instruction)
|
||
|
print(count,_from,_to)
|
||
|
_from -=1 # 1 based yo
|
||
|
_to -=1
|
||
|
for x in range(count):
|
||
|
value = crates[_from].pop(0)
|
||
|
crates[_to].insert(0, value)
|
||
|
return crates
|
||
|
|
||
|
def parse_instructions_pt2(crates, instructions):
|
||
|
for instruction in instructions:
|
||
|
count,_from,_to = scanf("move %d from %d to %d", instruction)
|
||
|
_from -=1 # 1 based yo
|
||
|
_to -=1
|
||
|
moving = crates[_from][:count]
|
||
|
print(instruction, moving)
|
||
|
for x in range(count):
|
||
|
crates[_from].pop(0)
|
||
|
for x in reversed(moving):
|
||
|
crates[_to].insert(0, x)
|
||
|
|
||
|
return crates
|
||
|
|
||
|
|
||
|
def part1(x):
|
||
|
crates, instructions = load_crates(x)
|
||
|
crates = to_lists(crates)
|
||
|
crates = parse_instructions(crates, instructions)
|
||
|
print([c[0] for c in crates])
|
||
|
|
||
|
|
||
|
def part2(x):
|
||
|
crates, instructions = load_crates(x)
|
||
|
crates = to_lists(crates)
|
||
|
crates = parse_instructions_pt2(crates, instructions)
|
||
|
print("".join([c[0] for c in crates]))
|
||
|
|
||
|
|
||
|
def main():
|
||
|
rows = load_rows(5)
|
||
|
#part1(rows)
|
||
|
part2(rows)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|