-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_5.py
58 lines (52 loc) · 1.74 KB
/
Day_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
f = open("puzzle.txt", "r")
rawData = f.read()
f.close()
#Changes based on input data
numLines = 9
indexInRow = numLines * 3 + (numLines-2)
cleanedData = rawData.split("\n")
#process first [numLines] of input to read the original crates order in data
def getCrates():
stackCount = 0
allCrates = [None] * numLines
for column in range(1, indexInRow, 4):
stack = []
for row in range(numLines-1, -1, -1):
if (cleanedData[row][column] != " "):
stack.append(cleanedData[row][column])
allCrates[stackCount] = stack
stackCount+=1
return allCrates
#Clean inputs of move commands from data
def getMoveInput():
splitSpaces = [list.split(" ") for list in cleanedData[numLines+2:]]
allMoves = []
for line in splitSpaces:
move = [int(line[1]), int(line[3]), int(line[5])]
allMoves.append(move)
return allMoves
#Iterates through and moves crates ONE at a time
def starOne():
allCrates = getCrates()
allMoves = getMoveInput()
for i in range (0, len(allMoves)):
for numPop in range (0, allMoves[i][0]):
popValue = allCrates[allMoves[i][1]-1].pop()
allCrates[allMoves[i][2]-1].append(popValue)
#Grabs top crates of each stack and returns it
return "".join([e.pop() for e in allCrates])
#Iterates through and moves crates MULTIPLE at a time
def starTwo():
allCrates = getCrates()
allMoves = getMoveInput()
for i in range (0, len(allMoves)):
cratesMoving = []
for numPop in range (0, allMoves[i][0]):
cratesMoving.append(allCrates[allMoves[i][1]-1].pop())
#Reverses list then appends.
cratesMoving.reverse()
allCrates[allMoves[i][2]-1] += cratesMoving
#Grabs top crates of each stack and returns it
return "".join([e.pop() for e in allCrates])
print(starOne())
print(starTwo())