Part 1
In part 1, we have a series of instructions for manipulating a string. One rotates the string around, one exchanges two characters by their indices and one for exchaning by value. We should follow the instructions for and report the resulting string.
Since there is no conditonal execution or jumping around, we can simply iterate over all instructions and implement the operation. For checking which operation to use, a regex or looking at the first character of the string was enough.
Part 2
In part 2, we should follow the instructions not once, but 1 billion times.
Instead of running the instruction so often, we check for a loop in the results. For that, we keep a list of previous result and check after each run if a result repeated. If yes, we can get the length of the loop from that and skip over most of the iterations.
No comments:
Post a Comment