\$\begingroup\$

Pada, 83 68 bytes

~.O~Ow~q~Owo~O~Oww~Q~qwo~q~O~wQ~q~w~q~q~Q~Ow~Q~Q~wo~q~w.~q~w.~.wO~qw

I believe this is optimal for a linear program (i.e. one which doesn't use the control flow operators ? and * ). It might be optimal overall, but I don't know how to go about making use of those additional operators in such a small amount of code (or how to explore the possibilities programmatically).

The language has recently undergone some changes after I started discussing it with the author by email. However, I have written a reference implementation for the current state of the language spec last week, so the above code is actually runnable.

Metagolf

Originally, I had used the output of my reference implementation to this challenge and created a hand-crafted solution based on that. However, that was just a heuristic approach.

So instead I wrote a solver in Mathematica which actually knows about the Pada data structures and operators to find an optimal solution. On average, it grows linearly with the length of the string (although some character combinations are a bit slower than others) and took about 1.5 hours for Hello, World! .

So how did I go about writing the solver. First, we notice that we only need to consider 6 operators: ~.oOqQ (plus the necessary w for each of the printed characters). Using the stacks or the bit locks isn't useful in linear code, and I don't believe that ? and * can be used effectively in less than 68 bytes.

Pada's state (ignoring the stacks and locks) consists of 7 switches and 8 bits, arranged like this:

/ / \ / \ / / 0 1 0 0 1 0 0 0

So that's 215 = 32768 possible states. My first preprocessing step was to set up a directed graph of states where each edge corresponds to a single operation. That is, the graph has 32768 vertices, each with out-degree 6 (one outgoing edge for each of the 6 operations under consideration). We can use this graph to find the shortest path between any two states (this graph alone can be quite useful for golfing Pada).

Now for each character, we want to reach a state where w prints that character. How many such states are there? w reads the byte from the bit it is dropped on (cyclically). So there are 8 possible rotations of the bits of the character which can all print that character. For each of those rotations, three switches are fixed (in order to make w drop in the correct position). This leaves 4 arbitrary switches. So we've got 8 * 24 = 128 possible states for each w in our code.

With those we can solve another graph problem: construct a graph which has a source vertex, then one "layer" for each character, and a sink vertex. The layers consist of the 128 states for each vertex, the source node corresponds to the initial state of the program (all switches to the left and all bits are zero). The sink node corresponds to no state in particular. We've got directed edges from every vertex in one layer to every vertex in the next layer, where the edge weight is the distance between the two states in our earlier graph. The weights of the edges from the last layer to the sink are all 0. That is, we can precompute all those edge weights. This is the most expensive step of the computation and took 1.5 hours for Hello, World! .

With this graph set up, we can find the shortest path from the source to the sink quite quickly (it took 0.05s on my machine). For Hello, World! the desired states are:

0, 16960, 22052, 13828, 13828, 30389, 12487, 8307, 27299, 23450, 18922, 22778, 18682, 18459

where the least significant 7 bits correspond to the switches and the most significant 8 bits to Pada's bits.

Now we go back to the first graph and find the actual edges (i.e. operations) corresponding to the shortest path between each pair of subsequent states, and end each of them with a w . Voilà, an optimal solution (based on the above assumptions).

Here is the full Mathematica if anyone ever wants to metagolf a different string in Pada: