|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +class SupplyStacks: |
| 4 | + ''' Represents stacks of supplies identified by characters. |
| 5 | +
|
| 6 | + Optional Arguments: |
| 7 | + filePath(pathlib.Path): The file in which to read supply stack information. |
| 8 | + stackData(str): The string that contains the stack information. |
| 9 | + ''' |
| 10 | + def __init__(self, filePath:Path=None, stackData:str=None): |
| 11 | + self.stacks = [] |
| 12 | + |
| 13 | + # If we have a string, we'll load from that first. |
| 14 | + if stackData: |
| 15 | + self.loadFromString(stackData) |
| 16 | + |
| 17 | + elif filePath: |
| 18 | + self.loadFromFile(filePath.resolve()) |
| 19 | + |
| 20 | + |
| 21 | + def __str__(self): |
| 22 | + # Iterating through our stacks to print our information |
| 23 | + # Just like how we're loading, we're going to write this backwards and print it out |
| 24 | + listHeaders = ' '.join([str(it+1) for it in range(len(self.stacks))]) |
| 25 | + output = [f' {listHeaders} '] |
| 26 | + |
| 27 | + # Finding tallest stack |
| 28 | + tallestStack = max([len(stack) for stack in self.stacks]) |
| 29 | + |
| 30 | + # We know how tall the message needs to be, so we create each of those rows |
| 31 | + for row in range(tallestStack): |
| 32 | + rowOut = '' |
| 33 | + # For each row, we iterate through the stacks |
| 34 | + for stack in self.stacks: |
| 35 | + # Writing either the data for the cargo or a blank line |
| 36 | + if row < len(stack): |
| 37 | + rowOut += f'[{stack[row]}] ' |
| 38 | + else: |
| 39 | + rowOut += ' ' |
| 40 | + |
| 41 | + # Moving to the next row |
| 42 | + output.append(f'{rowOut}\n') |
| 43 | + |
| 44 | + # Now that all our data is in our list, we reverse the list |
| 45 | + output.reverse() |
| 46 | + # And return it |
| 47 | + return ''.join(output) |
| 48 | + |
| 49 | + |
| 50 | + def loadFromFile(self, filePath:Path): |
| 51 | + ''' Reads in stack information from a file. |
| 52 | +
|
| 53 | + Expects a double line in between the stack information and the rest of the file. |
| 54 | +
|
| 55 | + Arguments: |
| 56 | + filePath(pathlib.Path): The path to the file to read our data from. |
| 57 | + ''' |
| 58 | + with open(filePath, 'r', encoding='UTF-8') as dataFile: |
| 59 | + self.loadFromString(dataFile.read().split('\n\n')[0]) |
| 60 | + |
| 61 | + |
| 62 | + def loadFromString(self, stackData:str): |
| 63 | + ''' Reads in stack information from a string. |
| 64 | +
|
| 65 | + Arguments: |
| 66 | + stackData(str): The string that contains the stack information. |
| 67 | + ''' |
| 68 | + # Most important information is how many stacks we have, so lets split and reverse our lines. |
| 69 | + stackDataLists = [_ for _ in stackData.split('\n') if _ != ''] |
| 70 | + maxLen = max([len(_) for _ in stackDataLists]) |
| 71 | + stackDataLists = [_.ljust(maxLen, ' ') for _ in stackDataLists] |
| 72 | + stackDataLists.reverse() |
| 73 | + |
| 74 | + # Now that we have our list count first, lets grab the number of lists we're working with |
| 75 | + numStacks = len(stackDataLists[0][1::4]) |
| 76 | + |
| 77 | + # Creating our blank 2D array to hold our supply information |
| 78 | + self.stacks = [[] for _ in range(numStacks)] |
| 79 | + # for _ in range(0, numStacks+1): |
| 80 | + # self.stacks.append([]) |
| 81 | + |
| 82 | + # Now we iterate through the rest of our data |
| 83 | + for row in stackDataLists[1:]: |
| 84 | + # Iterating through each of our stacks |
| 85 | + for stackNum, cargoName in enumerate(row[1::4]): |
| 86 | + # If our cargo isn't empty, append our cargo to the stack |
| 87 | + if cargoName != ' ': |
| 88 | + self.stacks[stackNum].append(cargoName) |
| 89 | + |
| 90 | + |
| 91 | + def move(self, sourceStack:int, resultStack:int, index:int=1): |
| 92 | + ''' Moves a piece of cargo from one stack to the other. |
| 93 | +
|
| 94 | + Arguments: |
| 95 | + sourceStack(int): The stack in which to pull the cargo from. |
| 96 | + resultStack(int): The stack in which to place our cargo. |
| 97 | + index(int): How the stack system was ordinated. i.e. was it a 0-index or 1-index system. |
| 98 | + Default: 1 |
| 99 | + ''' |
| 100 | + cargo = self.stacks[sourceStack - index].pop() |
| 101 | + self.stacks[resultStack - index].append(cargo) |
| 102 | + |
| 103 | +def solve(filePath:Path) -> str: |
| 104 | + ''' Finds the sum of priorities in a given file. |
| 105 | +
|
| 106 | + Arguments: |
| 107 | + filePath(pathlib.Path): The filepath in which to read data from. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + A string with the name of all the supply stack tops |
| 111 | + ''' |
| 112 | + # Opening our filepath and splitting our cargo stacks from our commands |
| 113 | + with open(filePath, 'r', encoding='UTF-8') as dataFile: |
| 114 | + cargoData, commands = dataFile.read().split('\n\n') |
| 115 | + |
| 116 | + # Now, lets create our supply stacks |
| 117 | + supplyStacks = SupplyStacks(stackData=cargoData) |
| 118 | + |
| 119 | + # Iterating through our commands and feeding them to the supply stack |
| 120 | + for command in commands.splitlines(): |
| 121 | + count, source, result = [int(_) for _ in command.split(' ')[1::2]] |
| 122 | + for _ in range(count): |
| 123 | + supplyStacks.move(source, result, index=1) |
| 124 | + |
| 125 | + # Printing our supplyStacks to get our result |
| 126 | + print(supplyStacks) |
| 127 | + |
| 128 | + # Returning a string of the top of all our stacks |
| 129 | + return ''.join([stack[-1] for stack in supplyStacks.stacks]) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + |
| 134 | + print('Executing main function') |
| 135 | + supplyStacks = SupplyStacks(Path() / 'Day5' / 'example.txt') |
| 136 | + print(supplyStacks) |
| 137 | + pass |
0 commit comments