Day 5! I did things a bit differently this time. I created component to hold each stack (an array of integers), and then had an actor spawn as many of those components as it needed. The component has Push Front and Push Back methods (not shown, they are as simple as you might expect), and a Pop Back which I will show to help ease you gently into what will come later.

The components are kept in an array on the parent actor, and constructed as needed, as the initial stacks are set up:

This Add To Column event is called during the initial stack set up which is also read from the puzzle input in an method called from Begin Play:

With the components set up, one for each stack, parsed from the puzzle input, the remaining lines from the puzzle are processed one at a time in the actor Event Tick:

Note that Branch-False execution path, we will come back to that. Process Line does all the heavy lifting in horrible graph that handles both parsing the line, and solving parts 1 and 2 of the problem, with its behavior switched on a bool. Absolutely criminal. Disgusting. Too late to change it now.

The main puzzle input was 512 lines, so capped by default at 60fps it finishes in just under 10s. Long enough to be annoying but not annoying enough to do anything about it.
Once all the lines are processed, the stack tops are collated back into a string and passed out to the UI via an Event Dispatcher, and the actor ticking turned off.

Note the “Int to String as Ascii” node. While Epic provide the GetCharacterasNumber I used in the Initial Parse, they provide no Blueprint support that I could find to go the other way, so I added that as the only C++ in todays solution:
// in AoC22BPFunctionLibrary.h // Converts the number to a Character and returns it as a string UFUNCTION(BlueprintCallable) static FString IntToStringAsAscii(int Number); // in AoC22BPFunctionLibrary.cpp FString UAoC22BPFunctionLibrary::IntToStringAsAscii(int Number) { FString result; result.AppendChar(Number); return result; }
In hindsight that really should be BlueprintPure.
This is all kicked of from some UMG UI that spawns the actor twice, once for each part, and binds to that ResultFound event.
1 thought on “Advent of Code in Unreal Engine Day 5”