Skip to main content
2 of 5
added 872 characters in body
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Jelly, (26?) 27 bytes

iⱮ’“¡©“®¢‘xⱮj€12b4;,a"4,1ɗF

A dyadic Link that accepts the starting data stack in reverse on the left and the final data stack in reverse on the left and yields a list of integers:

0 >r
1 r>
2 swap
3 dup
4 drop

Try it online! (Footer formats like the question does.)

Or see the Python suite of test results.

How?

This builds the reverse of the final data stack in the return stack one item at a time by "copying" the item from the data stack, leaving the data stack unaffected, then it drops all of the original items from the data stack and pulls all the copied items back.

To "copy" an item it pushes all those above it, then repeatedly pulls and swaps so that all the original main items are back on the data stack with the required item on top, then duplicates and pushes and then performs the conjugate of everything before the duplication to get main back to how it started.

For example, to push d from abcd... (where a is on top):

>r >r >r r> swap r> swap r> swap dup >r swap >r swap >r swap >r r> r> r>

...or

(>r)*3 (swap r>)*3 (dup >r) (swap >r)*3 (>r)*3

Where 3 is three because there are three items on top of d.

Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282