Human Resource Machine/Year 28

Strategy
This level can be particularly challenging, especially for non-programmers. You need to compare three numbers to each other to determine what order they should be placed in. The trick is not to find yourself subtracting all three numbers from each other. Instead, you can break this problem up into to major parts: For the first step, all we want to do is compare the first two numbers, and then place the smaller one in box 0, and the bigger one in box 1. That will set you up for the second step. Now you can tell where the third number ranks because you have the first two in the correct order. Either is will be smaller than the small number, or it will be between the two numbers, or it will be bigger than the big number. At that point, you can output the numbers in the order you have determined.
 * 1) Start with the first two numbers and decide which one is smaller.
 * 2) Take the third number and decide whether it is smaller than both, smaller than one, or bigger than both numbers.

For the comparison of the two numbers, this program will employ the step-saving technique of subtracting the number before copying it somewhere, and then restoring the number by adding the other number back in. (See the optimizing notes for ../Year 23/ for more explanation.)

a: b:   INBOX COPYTO  5 INBOX SUB     5 JUMPN   c    ADD      5 COPYTO  1 COPYFROM 5 COPYTO  0 JUMP    d c:    ADD      5 COPYTO  0 COPYFROM 5 COPYTO  1 d:   INBOX SUB     0 JUMPN   f    ADD      0 SUB     1 JUMPN   e    ADD      1 COPYTO  6 COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 6 OUTBOX JUMP    b e:    ADD      1 COPYTO  6 COPYFROM 0 OUTBOX COPYFROM 6 OUTBOX JUMP    g f:    ADD      0 OUTBOX COPYFROM 0 OUTBOX g:   COPYFROM 1 OUTBOX JUMP    a

Optimizing
The size optimized solution of The Tesseract with 33 commands and 123 steps:

JUMP    c a: b:     COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 2 OUTBOX c:    INBOX COPYTO  2 INBOX COPYTO  1 INBOX COPYTO  0 d:    SUB      1 JUMPN   e     COPYTO   1 COPYFROM 0 SUB     1 COPYTO  0 ADD     1 COPYTO  1 e:    COPYFROM 1 SUB     2 JUMPN   b     JUMPZ    a     COPYTO   2 COPYFROM 1 SUB     2 COPYTO  1 ADD     2 COPYTO  2 COPYFROM 0 JUMP    d

This solution avoids making cases whether a<b or b<a etc. Instead, it automatically puts in order the two first elements using arithmetic operations to swap if needed (using the a-b term previously used for comparison, saving one more command) thus reducing the number of commands used. Then it does the same for the two last elements, and finally, if needed, with the two first elements again. This process is called a cocktail shaker sort.

The size optimized solution of Gimlao from Steamcommunity.com with 34 commands and 134 steps:

a:   INBOX COPYTO  0 INBOX COPYTO  1 INBOX COPYTO  2 b:   COPYFROM 0 SUB     1 JUMPN   c    JUMPZ    d    COPYFROM 1 COPYTO  5 COPYFROM 0 COPYTO  1 COPYFROM 5 COPYTO  0 c: d:   COPYFROM 1 SUB     2 JUMPN   e    JUMPZ    f    COPYFROM 2 COPYTO  5 COPYFROM 1 COPYTO  2 COPYFROM 5 COPYTO  1 JUMP    b e: f:    COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 2 OUTBOX JUMP    a

The size optimized solution of eMicify from Steamcommunity.com with 34 commands and 125 steps:

a:   INBOX COPYTO  0 INBOX COPYTO  1 INBOX COPYTO  2 b:   SUB      1 JUMPN   c    COPYFROM 1 JUMP    d c:    COPYFROM 2 COPYTO  5 COPYFROM 1 COPYTO  2 COPYFROM 5 COPYTO  1 d:   SUB      0 JUMPN   e    JUMP     f e:    COPYFROM 1 COPYTO  5 COPYFROM 0 COPYTO  1 COPYFROM 5 COPYTO  0 COPYFROM 2 JUMP    b f:    COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 2 OUTBOX JUMP    a

The size optimization from elfire with 32 commands and 132 steps:

a:   INBOX COPYTO  0 INBOX COPYTO  2 INBOX COPYTO  1 b:   SUB      0 JUMPN   c    COPYFROM 0 COPYTO  5 COPYFROM 1 COPYTO  0 COPYFROM 5 COPYTO  1 c:   COPYFROM 2 SUB     1 JUMPN   d    JUMPZ    d    COPYFROM 2 COPYTO  5 COPYFROM 1 COPYTO  2 COPYFROM 5 COPYTO  1 JMP     b d:    COPYFROM 2 OUTBOX COPYFROM 1 OUTBOX COPYFROM 0 OUTBOX JMP     a

The speed optimized solution of McPunchins from his guide on the Steam community with 66 commands and 75 steps.

a: b: c: d: e: f:    INBOX COPYTO  0 INBOX COPYTO  1 SUB     0 JUMPN   g     JUMP     k g:     INBOX COPYTO  2 SUB     1 JUMPN   h     JUMP     i h:     ADD      1 OUTBOX COPYFROM 1 OUTBOX COPYFROM 0 OUTBOX JUMP    d i:     COPYFROM 1 OUTBOX COPYFROM 2 SUB     0 JUMPN   j     COPYFROM 0 OUTBOX COPYFROM 2 OUTBOX JUMP    f j:     COPYFROM 2 OUTBOX COPYFROM 0 OUTBOX JUMP    e k:     INBOX COPYTO  2 SUB     1 JUMPN   l     COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 2 OUTBOX JUMP    c l:     ADD      1 JUMPN   p     SUB      0 JUMPN   n m:     COPYFROM 0 OUTBOX COPYFROM 2 OUTBOX COPYFROM 1 OUTBOX JUMP    b n: o:     ADD      0 OUTBOX COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX JUMP    a p:     SUB      0 JUMPN   o     JUMP     m

McPunchins' solution can be optimized further (55 commands and 72 steps) by re-ordering some of the segments of code:

JUMP    c a:    ADD      0 OUTBOX COPYFROM 0 OUTBOX COPYFROM 1 b:   OUTBOX c:   INBOX COPYTO  0 INBOX COPYTO  1 SUB     0 JUMPN   d    INBOX COPYTO  2 SUB     1 JUMPN   g    COPYFROM 0 OUTBOX COPYFROM 1 OUTBOX COPYFROM 2 JUMP    b d:    INBOX COPYTO  2 SUB     1 JUMPN   f    COPYFROM 1 OUTBOX COPYFROM 2 SUB     0 JUMPN   e    COPYFROM 0 OUTBOX COPYFROM 2 JUMP    b e:    COPYFROM 2 OUTBOX COPYFROM 0 JUMP    b f:    ADD      1 OUTBOX COPYFROM 1 OUTBOX COPYFROM 0 JUMP    b g:    ADD      1 SUB     0 JUMPN   a    COPYFROM 0 OUTBOX COPYFROM 2 OUTBOX COPYFROM 1 JUMP    b