

If this number is a zero, we're pretty much done. Then we'll grab the second number from the INBOX. In this way, we've already handled what the sum is if we're multiplying the number by one. If that's not the case, we'll take whatever the value is, and write it to both box 1 and box 2. If it is, we're going to write that zero into box 2, grab the next value off the IN conveyor and throw it away, and then proceed with the normal finishing condition of grabbing whatever is in box 2 (a zero in this case) and putting it in the OUTBOX. Right away, we're going to ask if the first number we pull of the conveyor is zero. Instead, we're going to try to set up the problem so that the initialization won't be necessary.Īgain, we're always going to assume that the answer will be coming from box 2, and we're going to leverage that fact early on. We'll be using the same boxes for the same purpose as above, only we won't be initializing anything.
#Human resource machine ending code#
In order to do this, we're going to need to write special case code to handle certain situations, but we need to do it with as few jumps or decisions as possible. Reaching the desired speed of an average of 109 steps is a challenge indeed. If the second number is zero, it will become negative before anything ever gets added to the initial value of zero in box 2, which is what we'll send to the OUTBOX. If the first number is zero, then we just end up adding zero to itself over and over again until the second number becomes negative. This works well for the obvious cases, albeit inefficiently, but it does handle the two edge cases when either number might be zero. Then we jump back to bumping box 0 down and test if we're finished again. So we'll grab the current sum from box 2, add the contents of box 1, and store the answer back in box 2.

If it's not, we have some addition to do. We'll grab whatever is in box 2 and put it in the OUTBOX and continue. The number in box 0 will get bumped down, and if it's negative, that's how we know we're done. Then we'll grab the first two numbers off the IN conveyor and cache them in boxes 1 and 0 respectively. We're going to initialize Box 2 with the starting value of zero, which we can copy from box 9. When we're ready to hit the INBOX, we're going to do what's known as initialization. We'll skip over those lines the first time we run. Since the summed up number will be stored in box 2, the top of our program can be fetching the number from box 2, and bringing it to the OUTBOX. Box 0 will be where we store the other number that we're multiplying by, and it will be the number we bump down while we count how many times we add. Box 1 will be where we cache that number so that we know how much to add each time. We'll start by assuming that three boxes are going to used for the following tasks: Box 2 will be where we hold onto the sum of the number we're adding to itself. Since this is another case where you can't complete the size challenge and the speed challenge with one single program, the solutions will be split out again.ġ5 instructions can be a tough number to hit on this challenge, but it can be done with enough proper setup. But if the zero is the first number, you still need to grab the next number off the IN conveyor or your answer will be wrong. If the second number you pick up is the zero, put it on the OUTBOX. Naturally when either number is a zero, you want to simply put a zero in the OUTBOX and move on to the next two numbers. This is what's known as an "edge case," a situation that runs outside the normal circumstance that you have to write extra code to specifically handle. You don't have to worry about negative numbers, but there is another concern that you still have: multiplying by zero. Then you'll send the result to the OUTBOX. From there, you will proceed to add one of the values to itself, while you bump the other value down by one, and repeat this process until the value you're bumping down reaches zero. So to solve this problem, you will copy two values into two boxes.

Multiplying m by n is the same as adding m to itself n times. Fortunately, multiplication can be performed purely with addition. But you clearly don't have a multiplication instruction. If you do, the program that you write will reflect the labels that you've designated rather than the number of the box.Īs you can tell from the instructions, you must now write a multiplication program. If you want to mark a box with a particular purpose, you can click on the box and draw your own label for it. Like comments, labels are purely for your own benefit.
