Skip to content

Commit 482c64a

Browse files
committed
Added Day 5 Part 2
1 parent 154e676 commit 482c64a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Day5/Part2.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from pathlib import Path
2+
3+
from Day5 import Part1
4+
5+
6+
class SupplyStacks(Part1.SupplyStacks):
7+
def moveMany(self, sourceStack:int, resultStack:int, count:int=1, index:int=1):
8+
''' Moves a piece of cargo from one stack to the other.
9+
10+
Arguments:
11+
sourceStack(int): The stack in which to pull the cargo from.
12+
resultStack(int): The stack in which to place our cargo.
13+
count(int): Number of items to move to the new stack.
14+
index(int): How the stack system was ordinated. i.e. was it a 0-index or 1-index system.
15+
Default: 1
16+
'''
17+
cargo = self.stacks[sourceStack - index][-count:]
18+
del self.stacks[sourceStack - index][-count:]
19+
self.stacks[resultStack - index] += cargo
20+
21+
def solve(filePath:Path) -> str:
22+
''' Finds the sum of priorities in a given file.
23+
24+
Arguments:
25+
filePath(pathlib.Path): The filepath in which to read data from.
26+
27+
Returns:
28+
A string with the name of all the supply stack tops
29+
'''
30+
# Opening our filepath and splitting our cargo stacks from our commands
31+
with open(filePath, 'r', encoding='UTF-8') as dataFile:
32+
cargoData, commands = dataFile.read().split('\n\n')
33+
34+
# Now, lets create our supply stacks
35+
supplyStacks = SupplyStacks(stackData=cargoData)
36+
37+
# Iterating through our commands and feeding them to the supply stack
38+
for command in commands.splitlines():
39+
count, source, result = [int(_) for _ in command.split(' ')[1::2]]
40+
supplyStacks.moveMany(source, result, count=count, index=1)
41+
42+
# Printing our supplyStacks to get our result
43+
print(supplyStacks)
44+
45+
# Returning a string of the top of all our stacks
46+
return ''.join([stack[-1] for stack in supplyStacks.stacks])

0 commit comments

Comments
 (0)