Human Resource Machine/Year 16

Strategy
After solving the previous problems, this one is fairly straightforward. If the number is positive, you're obviously going to take it straight from the INBOX and over to the OUTBOX. The trick to this problem is how to properly deal with negative numbers so that you reverse the sign and organize your jumps properly.

The first thing to do after going to the INBOX is check if the number is negative. In that case, you'll jump to some point in the code. However, if the number is positive, you're going to want to jump over the code that deals with the negative numbers.

To reverse the sign of a negative number, all you need to do is subtract it from itself twice. The first subtraction will make the value zero, and then subtracting a negative from zero is like adding its positive value. Your first attempt will probably look like this:

a:   INBOX JUMPN   b    JUMP     c b:    COPYTO   0 SUB     0 SUB     0 c:   OUTBOX JUMP    a

Optimizing
As usual, the solution above meets the size challenge, but falls short of the speed challenge. With just a little work, we can rearrange the bottom of the program so that more instructions are at the top, and the jumps allow the code to run more efficiently.

We'll start by moving the OUTBOX above the INBOX command, and jumping to just above it if the number is positive. We'll move the sign correction code above that, and jump to the start of that code when the number is negative. And finally, we'll jump over all those instructions the very first time so that we start with the INBOX.

JUMP    c a:    COPYTO   0 SUB     0 SUB     0 b:   OUTBOX c:   INBOX JUMPN   a    JUMP     b

Additional Optimization
We can speed up the program even further by eliminating the first jump and adding some redundancy.

INBOX JUMPN   a    JUMP     c a: b:    COPYTO   0 SUB     0 SUB     0 c: d:   OUTBOX INBOX JUMPN   b    JUMP     d

Performance
Steps    Size      Speed Goal       8         36 Reached    8         33