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
Generally, moving jump calls above instead of below allows us to remove some unnecessary jump calls. We not only do this for outbox but also the bump commands. The code below meets size challenge of 16 and exceeds speed challenge of 393 with a score of 377.

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:   INBOX COPYTO  15 COPYFROM 14 COPYTO  18 COPYTO  19 JUMP    e c:    BUMPUP   19 d:   BUMPUP   18 e:   COPYFROM [18] JUMPZ   a    SUB      15 JUMPZ   c    JUMP     d

42 speed "think out of the box" solution
This solution works because letters on ground are always the same. 4 x "A", 5 x "B", 2 x "C" and 3 x "X"

BUMPUP  14  # first prepare constants "1" and "2" COPYTO  19 BUMPUP  19 loop: INBOX SUB     0   # test input character by subtraction "?" - "B" = JUMPN   c   # -1 => "C" JUMPZ   b   #  0 => "B" SUB     14  # decide between "A" and "X" JUMPZ   a    COPYFROM 1   # now just add some constants together # to make correct pre-calculated results SUB     4 c: b:   ADD      14 ADD     19 a:   ADD      19 OUTPUT JUMP    loop