From there, the program will run with one less instruction between the OUTBOX command and the INBOX command. So only for the very first time, you'll jump over that instruction and go straight to the INBOX instruction. However, the very first time you run this program, you can't go to the OUTBOX empty handed. You can accomplish this by placing the OUTBOX instruction before the INBOX instruction. It would be better if you didn't have to waste an instruction between the OUTBOX and the INBOX. Contributing to the problem is that fact that when you decide to go to the OUTBOX, you have to jump to it, and then jump back to the start of the program. However, if it's not zero, you must then jump back to the top of the program.Īside from Year 2, this is the first time that your program may be short enough, but not quick enough. If the value is zero, instead of jumping to the start of the program, you will jump down to the OUTBOX instruction. To do this, start with the program from Year 7 (you can copy it from that year, and paste it in this year.) Only now you will add another jump instruction. Move the zeros and ignore everything else. Instead of ignoring the zeros and moving everything else, you will do the opposite. Otherwise, you can add the first number back and jump straight to the OUTBOX command.This is simply the inverse problem of Year 7. Now they want you to take a pair of items off the IN. Note that you cannot instruct the worker to COPYTO a box if he/she is not holding anything. To get a little more speed out of it, you can also move the COPYFROM command up, and only jump to it if the difference is negative. The instruction tells the worker to walk over to the box on the floor labeled with the specified number, and place a copy of the value in his/her hands into that box. However, you can still further optimize the program by doing what was mentioned earlier, and moving the OUTBOX command to the top of the program. The above program will meet both optimization conditions. So we can eliminate the need to copy the number to a box first, reducing the program from 11 instructions to 10. In order to get the second number back, you can simply add the first number to the difference, and you'll be holding the second number again. However, subtracting the first number from the second number can be reversed. You do this because if you decide that you want to bring the second number to the OUTBOX, you don't want to lose it. One area to focus on is the need to copy the second value into a box before subtracting the first from the second. So you need to find one instruction to remove. The above program is 11 instructions, and the challenge is to be 10 instructions or less. You may think by now that the trick to optimizing this program is to move the OUTBOX instruction, and you'd be partially correct, but that won't reduce the size of the program. Your first attempt may look like the following program, although it will fail both the size test and the speed test: Since you don't know which number you'll be bringing to the OUTBOX, you might want to store both numbers in boxes first. To write this program correctly, you will have to evaluate the difference between two numbers, and write two sections of code, one for when the first number is bigger and the other for when the second number is bigger. If the answer is positive, then the second number was bigger. If you subtract a first number from a second and the answer is negative, then you can conclude that the first number was bigger. You can figure this out by subtracting them. Your task now is to determine which of two numbers is bigger. This new jump command will expand your options a little bit. If it's zero or any positive value, the program will ignore the jump command, and continue on to the next command. If it is, the program will follow the jump. However, this command only jumps to a different instruction if, and only if, the value held in the worker's hands is negative. JUMP IF NEGATIVE This jump command works similarly to the JUMP IF ZERO command. This time around, you get a new jump command: JUMP IF NEGATIVE (abbreviated JUMPN in the program listings).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |