Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IntCode {
- constructor() {
- this.program = [99];
- this.input = [];
- this.outputBuffer = [];
- this.pointer = 0;
- }
- run (program, input) {
- this.program = program;
- this.input = input;
- while(this.pointer >= 0 && this.pointer < this.program.length) {
- this.tick();
- }
- console.log("Output: " + this.outputBuffer);
- }
- tick() {
- let operation = this.parseCommand();
- this.pointer = this.ops[operation.o](this.program, this.pointer, operation.m);
- }
- parseCommand() {
- let command = "" + this.program[this.pointer++];
- let operation = command.slice(-2);
- let modes = command.slice(0, command.length-2).split("").reverse();
- while(modes.length < 3) modes.push("0");
- modes = modes.map(m => parseInt(m, 10))
- operation = parseInt(operation, 10);
- return {o: operation, m: modes};
- }
- getMem(index, mode = 0) {
- return (mode ? this.program[index] : this.program[this.program[index]]);
- }
- setMem(index, value) {
- this.program[this.program[index]] = value;
- }
- getInput() {
- this.inputPointer = this.inputPointer || 0;
- if(this.inputPointer >= this.input.length) throw "Attempting to access empty input";
- return this.input[this.inputPointer];
- }
- output(value) {
- this.outputBuffer.push(value);
- }
- get ops () {
- return {
- 1: (p, i, m) => { // ADD
- let mp = 0;
- let r = this.getMem(i++, m[mp++]) + this.getMem(i++, m[mp++]);
- this.setMem(i++, r);
- return i;
- },
- 2: (p, i, m) => { // MUL
- let mp = 0;
- let r = this.getMem(i++, m[mp++]) * this.getMem(i++, m[mp++]);
- this.setMem(i++, r);
- return i;
- },
- 3: (p, i, m) => { // INPUT
- this.setMem(i++, this.getInput());
- return i;
- },
- 4: (p, i, m) => { // OUTPUT
- this.output(this.getMem(i++, m[0]));
- return i;
- },
- 5: (p, i, m) => { // JUMP IF TRUE
- let mp = 0;
- if(this.getMem(i++, m[mp++])) i = this.getMem(i++, m[mp++]);
- else ++i;
- return i;
- },
- 6: (p, i, m) => { // JUMP IF FALSE
- let mp = 0;
- if(!this.getMem(i++, m[mp++])) i = this.getMem(i++, m[mp++]);
- else ++i;
- return i;
- },
- 7: (p, i, m) => { // LESS THAN
- let mp = 0;
- let r = (this.getMem(i++, m[mp++]) < this.getMem(i++, m[mp++]) ? 1 : 0)
- this.setMem(i++, r);
- return i;
- },
- 8: (p, i, m) => { // EQUALS
- let mp = 0;
- let r = (this.getMem(i++, m[mp++]) == this.getMem(i++, m[mp++]) ? 1 : 0)
- this.setMem(i++, r);
- return i;
- },
- 99: (p, i, m) => { // END
- return -1;
- }
- }
- }
- }
- let TEST = [3,225,1,225,6,6,1100,1,238,225,104,0,1002,114,46,224,1001,224,-736,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1,166,195,224,1001,224,-137,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1001,169,83,224,1001,224,-90,224,4,224,102,8,223,223,1001,224,2,224,1,224,223,223,101,44,117,224,101,-131,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,80,17,225,1101,56,51,225,1101,78,89,225,1102,48,16,225,1101,87,78,225,1102,34,33,224,101,-1122,224,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1101,66,53,224,101,-119,224,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102,51,49,225,1101,7,15,225,2,110,106,224,1001,224,-4539,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,88,78,225,102,78,101,224,101,-6240,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,226,677,224,102,2,223,223,1006,224,329,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,359,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,374,101,1,223,223,1008,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,404,1001,223,1,223,1007,226,226,224,1002,223,2,223,1005,224,419,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,434,101,1,223,223,108,677,677,224,1002,223,2,223,1005,224,449,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,464,1001,223,1,223,108,226,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,1008,226,226,224,102,2,223,223,1005,224,494,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,509,1001,223,1,223,8,677,226,224,1002,223,2,223,1006,224,524,101,1,223,223,7,226,677,224,1002,223,2,223,1006,224,539,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,554,1001,223,1,223,7,226,226,224,1002,223,2,223,1006,224,569,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,584,101,1,223,223,1108,677,677,224,102,2,223,223,1006,224,599,1001,223,1,223,1008,677,226,224,1002,223,2,223,1005,224,614,1001,223,1,223,8,677,677,224,1002,223,2,223,1006,224,629,1001,223,1,223,107,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,659,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226];
- let computer = new IntCode();
- computer.run(TEST, [5])
Advertisement
Add Comment
Please, Sign In to add comment