Completed day 5 part 2

This commit is contained in:
2022-12-05 21:50:19 +00:00
parent 09006f233b
commit bafca07a3c

72
2022/Day-05/day01-2.py Normal file
View File

@@ -0,0 +1,72 @@
#!/usr/bin/python
stackIndex = []
numStacks = 0
instructions = []
# Move a given number of itmes from the top of one stack to the top of another.
def moveItems(num, fromStack, toStack):
stacks[fromStack - 1].reverse()
numMoved = 0
while numMoved < num:
stacks[toStack - 1].append(stacks[fromStack - 1][(num - 1) - numMoved])
del stacks[fromStack - 1][(num - 1) - numMoved]
numMoved += 1
stacks[fromStack - 1].reverse()
# Find the line containing the stack definitions and store it as a list.
with open('input.txt', 'r') as inputFile:
for line in inputFile:
if(line[1].isdigit()):
stackIndex = list(line.strip('\n'))
numStacks = len(line.strip().replace(' ', ''))
break
# Now use the indexes of those definitions to build lists for each stack.
with open('input.txt', 'r') as inputFile:
rawStacks = []
for i in range(len(stackIndex)):
rawStacks.append([])
for line in inputFile:
if(line[0] != '['):
break
for i in range(len(stackIndex)):
if(stackIndex[i].isdigit() and line[i] != ' '):
rawStacks[i].append(line[i])
# Ditch any empty lists.
stacks = []
for stack in rawStacks:
if(stack != []):
stack.reverse()
stacks.append(stack)
# Clean up the instructions and put them in lists we can use.
rawInstructions = []
for line in inputFile:
thisLine = (line.strip().replace('move','').replace('from','').replace('to','').split(' '))
cleanLine = []
for i in thisLine:
if(i != ''):
cleanLine.append(i)
instructions.append(cleanLine)
# Run through the instructions and call the move() function.
for i in instructions:
try:
numToMove = int(i[0])
fromStack = int(i[1])
toStack = int(i[2])
moveItems(numToMove, fromStack, toStack)
except IndexError:
pass
# Print the answer.
finalItems = []
for stack in stacks:
stack.reverse()
print(stack[0], end='')
print('\n', end='')