5.1 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			5.1 KiB
		
	
	
	
	
	
	
	
id, title, challengeType, forumTopicId
| id | title | challengeType | forumTopicId | 
|---|---|---|---|
| 5a23c84252665b21eecc8038 | Subleq | 5 | 302328 | 
Description
- Let A, B, and C be the value stored in the three consecutive words in memory starting at the instruction pointer.
- Advance the instruction pointer 3 words to point at the address after the one containing C.
- If A is -1, then a character is read from standard input and its code point stored in the address given by B. C is unused.
- If B is -1, then the number contained in the address given by A is interpreted as a code point and the corresponding character output. C is again unused.
- Otherwise, both A and B are treated as the addresses of memory locations. The number contained in the address given by A is subtracted from the number at the address given by B (and the result stored back in address B). If the result is zero or negative, the value C becomes the new instruction pointer.
- If the instruction pointer becomes negative, execution halts.
15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 72 101 108 108 111 44 32 119 111 114 108 100 33 10 0Which corresponds to something like this in a hypothetical assembler language:
start:
    zero, message, -1
    message, -1, -1
    neg1, start+1, -1
    neg1, start+3, -1
    zero, zero, start
zero: 0
neg1: -1
message: "Hello, world!\n\0"
Instructions
Tests
tests:
  - text: <code>Subleq</code> should be a function.
    testString: assert(typeof Subleq == 'function', '<code>Subleq</code> should be a function.');
  - text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.
    testString: assert(typeof Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]) == 'string', '<code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.');
  - text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.
    testString: assert.equal(Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]), "Hello, world!", '<code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.');
Challenge Seed
function Subleq(mem) {
  // Good luck!
}
Solution
function Subleq(mem) {
  var out = "";
  var instructionPointer = 0;
  do {
    var a = mem[instructionPointer];
    var b = mem[instructionPointer + 1];
    if (a === -1) {} else if (b === -1) {
      out += String.fromCharCode(mem[a]);
    } else {
      mem[b] -= mem[a];
      if (mem[b] < 1) {
        instructionPointer = mem[instructionPointer + 2];
        continue;
      }
    }
    instructionPointer += 3;
  } while ((instructionPointer >= 0));
  return out;
}