For Day 7 I have gone for an Actor & Component approach fully implemented in Blueprint. I will be misusing Unreal’s scene graph data structures as a proxy for the file-system hierarchy described by the puzzle. The actor has a component property (variable) I named “Current” that acts as a Cursor to the current location in the hierarchy as it is built.
It starts by processing the input line by line in the Actor Event Tick to build a tree of components:
The Run Command function checks the string and routes to 1 of 4 helper functions:
Move Outermost simply resets our cursor variable to the root component on the actor. Similarly, Move Out uses Get Attach Parent to move the cursor it its parent in the scene hierarchy. List and Move In are a bit more interesting. For List I have ended up with this weird looking graph that uses a while-loop over the “$ ls” command output, and calls Add Child for each entry:
Add Child creates a file system record (component) if it doesn’t already exist, and attaches it to the Current component:
The components being created are a new blueprint component type that have Name and Size property variables. I will add a Total Size function later.
I don’t treat directories any differently to files at this point. Get If Exists loops over all the child components of the Current cursor location, and checks for a name match:
Move In uses the Get If Exists function to set the Current cursor location. This all seems to work and with the file system data structure recreated from the puzzle input, I can tackle actually answering the puzzle.
The heavy lifting – and I know this could be a log more efficient if I used recursion and stored the subtotals in the file-entry component – is done in the Total Size function on the component:
Note the use of “Include All Descendants” ticked here on the Get Children Components node. This flattens the hierarchy for me and returns it as an array I can loop over trivially.
I can then use the same trick to answer both parts of the puzzle in one non-recursive loop:
The most painful part of all this? Figuring out why the math didn’t make sense when I had an extra 0 on the end of the total space required constant.
I have some travel for work the next couple days so I wont be able to look at days 8 & 9 until I get back. Until then, have fun coding!