AdventOfCode/2022/day5/day5_part1.py

46 lines
1.4 KiB
Python
Raw Normal View History

2023-12-09 22:06:15 +01:00
import sys
from pprint import pprint
arr = []
if sys.argv[1] == "test_input":
stacks = [['N','Z'],['D', 'C','M'], ['P']]
else:
stacks = [['Q','F','L','S','R'],['T','P','G','Q','Z','N'],['B','Q','M','S'],['Q','B','C','H','J','Z','G','T'],['S','F','N','B','M','H','P'],['G','V','L','S','N','Q','C','P'],['F','C','W'],['M','P','V','W','Z','G','H','Q'],['R','N','C','L','D','Z','G']]
with open(sys.argv[1],"r") as f:
line = [line.rstrip('\n') for line in f]
for i in range(0,len(line)):
arr.append([int(x) for x in line[i].split() if x.isdigit()])
pprint(arr)
pprint(stacks)
print('----')
for i in arr:
temp = []
#print('pre: ' + str(stacks))
#print('move ' + str(i[0]) + ' letter from stack ' + str(i[1]) + ' to stack ' + str(i[2]) )
for j in range(0,i[0]):
#print('range: ' + str(0) + ' to ' + str(i[0]))
#print('move ' + str(stacks[i[1]-1][0] + ' from '), end="")
#print('' + str(stacks[i[1]-1]), end="")
#print(' to ' + str(stacks[i[2]-1]))
temp += stacks[i[1]-1].pop(0)
if len(temp) > 1:
temp.reverse()
#print('stacks:: ' + str(temp))
#print(i[0])
if len(temp) != 0:
for l in temp:
stacks[i[2]-1].insert(0,l)
else:
print('somehow temp length is 0')
temp = []
#print('post: ' + str(stacks))
for i in stacks:
print(i[0],end="")
print()