AOC22/day5/main2.py

54 lines
1.6 KiB
Python

with open("day5\input.txt") as f:
data = f.readlines()
'''
[J] [F] [M]
[Z] [F] [G] [Q] [F]
[G] [P] [H] [Z] [S] [Q]
[V] [W] [Z] [P] [D] [G] [P]
[T] [D] [S] [Z] [N] [W] [B] [N]
[D] [M] [R] [J] [J] [P] [V] [P] [J]
[B] [R] [C] [T] [C] [V] [C] [B] [P]
[N] [S] [V] [R] [T] [N] [G] [Z] [W]
1 2 3 4 5 6 7 8 9
move 2 from 4 to 6
Move amount from target to destination, In one go, (pop(amount),append)
Give [-1] of each stack at the end.
'''
#Create lists
cargo_processed = False
cargo_hold = []
for line in data:
if line == "\n":
continue
if not len(cargo_hold):
for cargo_spot in line[1:-2:4]:
cargo_hold.append([])
if not cargo_processed:
for position,char in enumerate(line[1:-2:4]):
if char == '1':
#Done parsing. switch to stacking
for stack in cargo_hold:
stack.reverse()
print(cargo_hold)
cargo_processed = True
break
if char == ' ':
continue
cargo_hold[position].append(char)
else:
#Start reading instructions
# Example move 2 from 4 to 6
move_command = [int(s) for s in line.split() if s.isdigit()]
reverselist = [cargo_hold[move_command[1]-1].pop() for idx in range(move_command[0])]
reverselist.reverse()
cargo_hold[move_command[2]-1] = cargo_hold[move_command[2]-1] + reverselist
for cargo in cargo_hold:
print(cargo[-1])
#VRQWPDSGP