2022-12-09 16:43:00 +00:00
|
|
|
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:
|
2022-12-12 07:41:14 +00:00
|
|
|
if row == "":
|
2022-12-09 16:43:00 +00:00
|
|
|
done_crates = True
|
|
|
|
else:
|
|
|
|
crates.append(row)
|
|
|
|
crates.pop()
|
|
|
|
return crates, instructions
|
|
|
|
|
2022-12-12 07:41:14 +00:00
|
|
|
|
2022-12-09 16:43:00 +00:00
|
|
|
def to_lists(crates):
|
|
|
|
parsed = []
|
|
|
|
reg = re.compile(r"[\([{})\]]")
|
|
|
|
for row in crates:
|
2022-12-12 07:41:14 +00:00
|
|
|
parsed.append([x for x in reg.sub(" ", row)])
|
2022-12-09 16:43:00 +00:00
|
|
|
parsed = list(zip(*parsed[::-1]))
|
|
|
|
parsed = list(zip(*parsed[::-1]))
|
|
|
|
parsed = list(zip(*parsed[::-1]))
|
2022-12-12 07:41:14 +00:00
|
|
|
cleaned1 = [[x for x in y if x.strip()] for y in parsed if y]
|
2022-12-09 16:43:00 +00:00
|
|
|
cleaned2 = [x for x in cleaned1 if x != []][::-1]
|
|
|
|
return cleaned2
|
|
|
|
|
2022-12-12 07:41:14 +00:00
|
|
|
|
2022-12-09 16:43:00 +00:00
|
|
|
def parse_instructions(crates, instructions):
|
|
|
|
for instruction in instructions:
|
2022-12-12 07:41:14 +00:00
|
|
|
count, _from, _to = scanf("move %d from %d to %d", instruction)
|
|
|
|
print(count, _from, _to)
|
|
|
|
_from -= 1 # 1 based yo
|
|
|
|
_to -= 1
|
2022-12-09 16:43:00 +00:00
|
|
|
for x in range(count):
|
|
|
|
value = crates[_from].pop(0)
|
|
|
|
crates[_to].insert(0, value)
|
|
|
|
return crates
|
|
|
|
|
2022-12-12 07:41:14 +00:00
|
|
|
|
2022-12-09 16:43:00 +00:00
|
|
|
def parse_instructions_pt2(crates, instructions):
|
|
|
|
for instruction in instructions:
|
2022-12-12 07:41:14 +00:00
|
|
|
count, _from, _to = scanf("move %d from %d to %d", instruction)
|
|
|
|
_from -= 1 # 1 based yo
|
|
|
|
_to -= 1
|
2022-12-09 16:43:00 +00:00
|
|
|
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
|
2022-12-12 07:41:14 +00:00
|
|
|
|
2022-12-09 16:43:00 +00:00
|
|
|
|
|
|
|
def part1(x):
|
|
|
|
crates, instructions = load_crates(x)
|
|
|
|
crates = to_lists(crates)
|
|
|
|
crates = parse_instructions(crates, instructions)
|
|
|
|
print([c[0] for c in crates])
|
|
|
|
|
2022-12-12 07:41:14 +00:00
|
|
|
|
2022-12-09 16:43:00 +00:00
|
|
|
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)
|
2022-12-12 07:41:14 +00:00
|
|
|
# part1(rows)
|
2022-12-09 16:43:00 +00:00
|
|
|
part2(rows)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|