Human Resource Machine/Year 32

Strategy
Although you can quickly glance at the floor and determine the number of instances of each letter, your worker is somewhat blind to the answer, and must evaluate each letter every time as they are accessed from the INBOX. In essence, the worker must walk over every letter on the floor every time, and count up how many of a target letter he or she finds.

This may seem counter-intuitive, but in order to determine of one letter is the same as another, you simply subtract them. If two letters are the same, subtracting one from the other will equal zero. This is because each letter has a numerical value associated with it. You can assume that A is equal to 1, B equals 2, and so on until you reach Z which equals 26. So your test will involve subtracting each letter on the floor from your target letter to see if they are equal.

To set this problem up properly, the 0 in box 14 is going to be very important, and serve several functions. First and foremost, it signifies that you're done checking letters on the floor, so you do not want to change the value of it. However, you'll want to make not one, but two copies of it before you begin. The first copy will serve as the indirect index to what box you want to check next. The second copy will be your count for how many letters matched. So to start, you should make two copies of that 0 elsewhere.

Then you'll grab a letter from the INBOX and store it somewhere. Then starting with box 0, you'll make your way through each box, one at a time, until you reach the zero in box 14. For each letter in a box that you examine, you'll subtract from the target letter, and if the result is zero, you'll bump up your count by one. Then you'll bump up the indirect index and repeat.

JUMP    b a:    COPYFROM 19 OUTBOX b:   COPYFROM 14 COPYTO  19 COPYTO  18 INBOX COPYTO  15 c:   COPYFROM [18] JUMPZ   a    SUB      15 JUMPZ   d    JUMP     e d:    BUMPUP   19 e:   BUMPUP   18 JUMP    c

Optimizing
The above program meets the size requirement, but even with the pre-optimization step of moving the OUTBOX command to the top and jumping over it in the beginning, it falls short of the speed requirement. This is because there are a lot of extra jumps places around the increment bumps near the bottom of the program. You can reduce those by moving them up and being more efficient with your jumps. But to do this, you must also add an extra step that decreases the initial value of the 0s you place by one, so that you end up initializing the values with -1. This is fine because on the first run through of each letter, you will bump them up by one and bring them back to 0. The code below not only meets the size requirement, it exceeds the speed requirement.

Note: Players have determined that it is possible to write programs that execute relatively quickly (in the ballpark of 23 to 31 steps on average), but only by making several assumptions about the arrangement of letters on the floor which may not always be true. These solutions, however, will only work with specific floor layouts and are not considered universal solutions which pass the stage.

JUMP    b a:    COPYFROM 19 OUTBOX b:   COPYFROM 14 COPYTO  19 BUMPDN  19 COPYTO  18 INBOX COPYTO  15 c:   BUMPUP   19 d:   BUMPUP   18 COPYFROM [18] JUMPZ   a    SUB      15 JUMPZ   c    JUMP     d