Skip to content

Instantly share code, notes, and snippets.

@FelicitusNeko
Last active December 17, 2019 22:06
Show Gist options
  • Select an option

  • Save FelicitusNeko/21a34a823f4ba9f8f82da28d467973c3 to your computer and use it in GitHub Desktop.

Select an option

Save FelicitusNeko/21a34a823f4ba9f8f82da28d467973c3 to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 13
class IntOpcodeMachine {
constructor(pgm) {
this.pgm = pgm.split(',').map(i => parseInt(i));
this.originalPgm = this.pgm.slice(0);
this.position = 0;
this.relBase = 0;
this.cmdSize = { 1: 4, 2: 4, 3: 2, 4: 2, 5: 3, 6: 3, 7: 4, 8: 4, 9: 2, 99: 1 }
this.input = []; this.output = [];
}
run() {
let isRunning = true;
let retval = true;
let runCount = 0;
while (isRunning && this.position < this.pgm.length) {
let noun = 0, verb = 0;
let opcode = this.pgm[this.position] % 100;
if (this.cmdSize[opcode] === undefined) throw new Error(`Invalid opcode ${opcode} at position ${this.position}`);
let mode = [];
for (let x = 2; x <= this.cmdSize[opcode]; x++) mode.push(Math.round(this.pgm[this.position] / Math.pow(10, x)) % 10);
switch (opcode) {
case 1: // Add values
noun = this.getWithMode(this.position + 1, mode[0]);
verb = this.getWithMode(this.position + 2, mode[1]);
this.setWithMode(this.position + 3, mode[2], noun + verb);
break;
case 2: // Multiply values
noun = this.getWithMode(this.position + 1, mode[0]);
verb = this.getWithMode(this.position + 2, mode[1]);
this.setWithMode(this.position + 3, mode[2], noun * verb);
break;
case 3: // Get from input stack
if (this.input.length === 0) return false;
this.setWithMode(this.position + 1, mode[0], this.input.shift());
break;
case 4: // Push to output stack
this.output.push(this.getWithMode(this.position + 1, mode[0]));
break;
case 5: // Jump if true
if (this.getWithMode(this.position + 1, mode[0]) !== 0) this.position = this.getWithMode(this.position + 2, mode[1]) - 3;
break;
case 6: // Jump if false
if (this.getWithMode(this.position + 1, mode[0]) === 0) this.position = this.getWithMode(this.position + 2, mode[1]) - 3;
break;
case 7: // Evaluate less than
this.setWithMode(this.position + 3, mode[2], (this.getWithMode(this.position + 1, mode[0]) < this.getWithMode(this.position + 2, mode[1]) ? 1 : 0));
break;
case 8: // Evaluate equal
this.setWithMode(this.position + 3, mode[2], (this.getWithMode(this.position + 1, mode[0]) === this.getWithMode(this.position + 2, mode[1]) ? 1 : 0));
break;
case 9: // Adjust relative base
this.relBase += this.getWithMode(this.position + 1, mode[0]);
break;
case 99: // End program
isRunning = false; break;
default: throw new Error(`Invalid opcode ${opcode} at position ${this.position}`);
}
this.position += this.cmdSize[opcode];
if (runCount > 50000) {
console.debug(this.output);
console.debug(this.toString());
throw new Error('Run length cap exceeded');
}
}
return retval;
}
addInput(data) {
if (Array.isArray(data)) this.input = this.input.concat(data);
else this.input.push(data);
}
getOutput() { return this.output.shift(); }
getAllOutput() {
let retval = this.output;
this.output = [];
return retval;
}
zeroFillToPos(pos) {
while (this.pgm.length <= pos) this.pgm.push(0);
}
setAt(pos, data) {
this.zeroFillToPos(pos);
this.pgm[pos] = data;
}
getAt(pos) {
this.zeroFillToPos(pos);
return this.pgm[pos];
}
getModePos(pos, mode) {
switch (mode) {
case 0: return this.pgm[pos];
case 1: return pos;
case 2: return this.pgm[pos] + this.relBase;
default: throw new Error(`Invalid access mode #${mode}`);
}
}
setWithMode(pos, mode, data) {
let modePos = this.getModePos(pos, mode);
this.zeroFillToPos(modePos);
this.pgm[modePos] = data;
}
getWithMode(pos, mode) {
let modePos = this.getModePos(pos, mode);
this.zeroFillToPos(modePos);
return this.pgm[modePos];
}
goto(pos) { this.position = pos; }
restart() { this.pgm = this.originalPgm.slice(); this.position = 0; this.input = []; this.output = []; }
toString() { return this.pgm.join(','); }
}
const populateScreen = screenData => {
let mapData = [];
let counts = [0, 0, 0, 0, 0];
const expand = (array, size, containArrays) => { while (array.length <= size) array.push(containArrays ? [] : 0); }
for (let x = 0; x < screenData.length; x += 3) {
expand(mapData, screenData[x + 1], true);
expand(mapData[screenData[x + 1]], screenData[x], false);
mapData[screenData[x + 1]][screenData[x]] = screenData[x + 2];
counts[screenData[x + 2]]++;
}
return { mapData, counts };
}
let bigData = `1,380,379,385,1008,2655,793190,381,1005,381,12,99,109,2656,1101,0,0,383,1102,0,1,382,20101,0,382,1,20102,1,383,2,21102,37,1,0,1105,1,578,4,382,4,383,204,1,1001,382,1,382,1007,382,42,381,1005,381,22,1001,383,1,383,1007,383,24,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1101,0,-1,384,1106,0,119,1007,392,40,381,1006,381,161,1102,1,1,384,21001,392,0,1,21101,0,22,2,21102,0,1,3,21102,138,1,0,1105,1,549,1,392,384,392,20102,1,392,1,21101,22,0,2,21102,3,1,3,21101,161,0,0,1105,1,549,1102,0,1,384,20001,388,390,1,21002,389,1,2,21101,180,0,0,1106,0,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21001,389,0,2,21101,0,205,0,1105,1,393,1002,390,-1,390,1102,1,1,384,21002,388,1,1,20001,389,391,2,21101,0,228,0,1106,0,578,1206,1,261,1208,1,2,381,1006,381,253,21001,388,0,1,20001,389,391,2,21101,253,0,0,1105,1,393,1002,391,-1,391,1102,1,1,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,279,0,0,1105,1,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21102,304,1,0,1106,0,393,1002,390,-1,390,1002,391,-1,391,1102,1,1,384,1005,384,161,20101,0,388,1,20102,1,389,2,21101,0,0,3,21101,338,0,0,1105,1,549,1,388,390,388,1,389,391,389,20102,1,388,1,21002,389,1,2,21102,4,1,3,21102,365,1,0,1106,0,549,1007,389,23,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,355,19,19,1,1,21,109,3,21201,-2,0,1,21202,-1,1,2,21102,1,0,3,21102,414,1,0,1106,0,549,21201,-2,0,1,21201,-1,0,2,21101,0,429,0,1106,0,601,2102,1,1,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2105,1,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,21201,-3,0,-7,109,-8,2106,0,0,109,4,1202,-2,42,566,201,-3,566,566,101,639,566,566,2102,1,-1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,42,593,201,-2,593,593,101,639,593,593,21001,0,0,-2,109,-3,2105,1,0,109,3,22102,24,-2,1,22201,1,-1,1,21101,509,0,2,21102,526,1,3,21102,1008,1,4,21102,630,1,0,1105,1,456,21201,1,1647,-2,109,-3,2106,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,2,2,0,2,0,2,2,0,2,2,0,2,2,2,2,2,2,0,2,2,2,0,2,0,2,2,0,2,2,0,2,2,2,0,0,2,2,0,0,1,1,0,2,2,2,0,0,0,2,0,0,2,2,0,2,0,2,0,2,2,0,0,2,2,0,2,2,2,0,2,0,2,2,2,2,0,2,2,0,2,0,1,1,0,2,2,2,2,2,0,0,0,0,2,0,2,2,2,0,2,2,0,0,0,2,0,0,2,0,0,2,0,2,2,0,0,2,0,0,2,0,2,0,1,1,0,2,0,0,2,2,2,2,2,2,0,2,0,2,2,0,0,0,0,2,0,0,2,2,2,0,2,0,0,0,0,2,2,2,2,0,2,0,2,0,1,1,0,0,2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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,73,91,79,27,84,58,31,29,97,58,15,10,67,88,80,10,1,3,95,62,30,42,69,5,42,17,47,33,88,95,98,31,66,63,22,32,40,11,95,47,11,12,83,74,32,5,17,1,81,84,29,88,4,93,37,69,94,69,22,89,34,7,76,9,54,53,82,57,43,22,70,80,92,76,55,81,53,37,21,23,49,23,85,23,80,97,59,9,62,94,3,29,87,98,33,7,60,88,29,58,24,39,2,35,20,94,21,90,42,60,85,92,51,17,67,7,90,84,59,79,10,32,29,44,2,88,86,3,57,66,11,25,11,70,95,8,32,24,8,32,80,57,96,89,85,88,2,51,46,80,24,19,41,81,62,4,98,92,33,32,50,27,75,16,90,57,89,77,82,72,67,35,34,29,59,14,74,26,60,37,88,87,50,35,12,27,73,66,42,16,84,84,15,64,46,4,9,73,85,42,79,10,49,88,78,41,69,71,92,32,35,13,84,71,82,18,68,97,45,37,87,20,28,20,10,2,30,26,21,97,56,91,60,38,12,71,73,55,71,24,59,79,37,62,12,75,65,72,19,23,69,47,86,55,66,78,87,29,59,6,63,7,32,84,80,74,41,60,58,4,70,61,5,82,11,45,79,56,7,48,62,4,46,88,83,31,96,97,18,91,68,38,31,24,64,3,14,1,85,10,52,97,92,81,36,1,2,27,28,56,75,32,88,46,98,49,22,80,83,93,41,12,35,70,81,57,53,92,20,49,76,77,8,50,93,35,81,15,22,46,14,54,53,48,74,32,19,91,39,61,61,39,55,57,50,64,27,33,84,1,81,89,70,24,28,28,60,30,16,28,66,96,51,38,86,23,29,49,5,95,61,7,20,41,31,84,59,21,21,49,62,9,30,87,83,65,89,29,61,3,13,14,9,71,87,78,48,7,84,69,62,57,43,82,31,76,4,79,46,91,11,6,30,87,34,10,2,50,66,5,77,87,57,43,84,84,79,75,42,10,14,3,32,21,24,68,75,12,36,64,18,91,14,91,80,52,50,1,50,37,74,41,11,1,93,79,50,94,51,37,52,21,84,57,73,44,2,77,81,54,48,82,76,63,14,50,1,87,94,89,89,81,84,47,72,37,94,95,62,76,49,47,40,43,86,16,17,59,8,13,31,89,65,80,29,70,40,34,33,64,42,68,75,58,69,23,49,68,16,43,78,54,66,49,17,14,90,7,56,7,23,13,50,70,17,83,9,57,11,28,55,11,14,5,47,4,9,94,17,64,98,83,89,58,91,74,62,77,62,2,11,84,61,23,60,37,40,73,30,35,8,24,28,56,7,50,81,31,21,41,87,10,56,26,86,66,77,43,69,80,47,74,36,13,29,14,83,68,88,96,52,93,62,82,62,89,74,35,70,68,75,69,35,22,50,10,8,18,18,2,54,75,64,20,65,81,62,60,98,19,40,97,6,57,21,63,43,80,39,25,56,46,70,42,83,9,42,5,64,56,16,79,39,23,12,9,47,3,96,47,32,43,91,12,6,98,47,91,34,17,5,93,91,22,32,47,39,1,11,75,73,15,36,46,41,26,98,43,46,49,20,60,43,13,71,92,58,62,19,17,30,71,97,43,14,62,27,71,48,50,96,80,13,13,39,98,38,80,55,37,13,96,88,15,37,26,84,12,80,46,85,36,50,86,76,63,2,85,52,14,28,71,32,66,95,22,54,56,33,22,97,57,30,20,60,86,34,65,21,47,38,38,53,81,79,45,65,65,28,89,48,9,13,41,3,62,20,61,47,65,11,11,18,76,93,80,72,74,61,7,5,35,13,64,86,56,4,51,13,63,94,79,12,27,59,94,73,59,36,32,19,10,24,29,65,67,2,67,87,26,64,28,5,60,95,3,97,70,98,57,68,51,68,57,96,56,78,75,32,89,67,52,46,93,16,82,92,75,5,35,50,2,84,57,32,24,71,58,60,49,56,15,74,28,73,61,12,4,38,46,86,92,26,68,98,20,76,94,28,14,53,68,15,38,5,81,21,24,33,11,78,83,19,3,80,98,57,38,97,11,9,45,29,33,34,7,47,32,28,14,65,66,84,12,47,59,17,82,11,63,6,68,73,4,87,85,42,24,12,67,57,27,15,21,53,96,73,36,57,12,9,72,11,33,27,9,92,21,96,25,87,45,1,31,90,56,11,37,8,87,13,74,40,17,20,78,17,94,12,54,21,67,15,16,41,44,77,44,88,64,14,47,54,45,21,20,77,41,5,97,51,43,81,44,65,64,33,7,793190`;
let machine = new IntOpcodeMachine(bigData);
machine.run();
console.log(populateScreen(machine.getAllOutput()));
‎‎​class IntOpcodeMachine {
constructor(pgm) {
this.pgm = pgm.split(',').map(i => parseInt(i));
this.originalPgm = this.pgm.slice(0);
this.position = 0;
this.relBase = 0;
this.cmdSize = { 1: 4, 2: 4, 3: 2, 4: 2, 5: 3, 6: 3, 7: 4, 8: 4, 9: 2, 99: 1 }
this.input = []; this.output = [];
}
run() {
let isRunning = true;
let retval = true;
let runCount = 0;
while (isRunning && this.position < this.pgm.length) {
let noun = 0, verb = 0;
let opcode = this.pgm[this.position] % 100;
if (this.cmdSize[opcode] === undefined) throw new Error(`Invalid opcode ${opcode} at position ${this.position}`);
let mode = [];
for (let x = 2; x <= this.cmdSize[opcode]; x++) mode.push(Math.round(this.pgm[this.position] / Math.pow(10, x)) % 10);
switch (opcode) {
case 1: // Add values
noun = this.getWithMode(this.position + 1, mode[0]);
verb = this.getWithMode(this.position + 2, mode[1]);
this.setWithMode(this.position + 3, mode[2], noun + verb);
break;
case 2: // Multiply values
noun = this.getWithMode(this.position + 1, mode[0]);
verb = this.getWithMode(this.position + 2, mode[1]);
this.setWithMode(this.position + 3, mode[2], noun * verb);
break;
case 3: // Get from input stack
if (this.input.length === 0) return false;
this.setWithMode(this.position + 1, mode[0], this.input.shift());
break;
case 4: // Push to output stack
this.output.push(this.getWithMode(this.position + 1, mode[0]));
break;
case 5: // Jump if true
if (this.getWithMode(this.position + 1, mode[0]) !== 0) this.position = this.getWithMode(this.position + 2, mode[1]) - 3;
break;
case 6: // Jump if false
if (this.getWithMode(this.position + 1, mode[0]) === 0) this.position = this.getWithMode(this.position + 2, mode[1]) - 3;
break;
case 7: // Evaluate less than
this.setWithMode(this.position + 3, mode[2], (this.getWithMode(this.position + 1, mode[0]) < this.getWithMode(this.position + 2, mode[1]) ? 1 : 0));
break;
case 8: // Evaluate equal
this.setWithMode(this.position + 3, mode[2], (this.getWithMode(this.position + 1, mode[0]) === this.getWithMode(this.position + 2, mode[1]) ? 1 : 0));
break;
case 9: // Adjust relative base
this.relBase += this.getWithMode(this.position + 1, mode[0]);
break;
case 99: // End program
isRunning = false; break;
default: throw new Error(`Invalid opcode ${opcode} at position ${this.position}`);
}
this.position += this.cmdSize[opcode];
if (runCount > 50000) {
console.debug(this.output);
console.debug(this.toString());
throw new Error('Run length cap exceeded');
}
}
return retval;
}
addInput(data) {
if (Array.isArray(data)) this.input = this.input.concat(data);
else this.input.push(data);
}
getOutput() { return this.output.shift(); }
getAllOutput() {
let retval = this.output;
this.output = [];
return retval;
}
zeroFillToPos(pos) {
while (this.pgm.length <= pos) this.pgm.push(0);
}
setAt(pos, data) {
this.zeroFillToPos(pos);
this.pgm[pos] = data;
}
getAt(pos) {
this.zeroFillToPos(pos);
return this.pgm[pos];
}
getModePos(pos, mode) {
switch (mode) {
case 0: return this.pgm[pos];
case 1: return pos;
case 2: return this.pgm[pos] + this.relBase;
default: throw new Error(`Invalid access mode #${mode}`);
}
}
setWithMode(pos, mode, data) {
let modePos = this.getModePos(pos, mode);
this.zeroFillToPos(modePos);
this.pgm[modePos] = data;
}
getWithMode(pos, mode) {
let modePos = this.getModePos(pos, mode);
this.zeroFillToPos(modePos);
return this.pgm[modePos];
}
goto(pos) { this.position = pos; }
restart() { this.pgm = this.originalPgm.slice(); this.position = 0; this.input = []; this.output = []; }
toString() { return this.pgm.join(','); }
}
const populateScreen = (screenData, previousState) => {
let { mapData, paddleX, ball, score } = previousState;
let counts = [0, 0, 0, 0, 0];
const expand = (array, size, containArrays) => { while (array.length <= size) array.push(containArrays ? [] : 0); }
for (let x = 0; x < screenData.length; x += 3) {
if (screenData[x] < 0) switch (screenData[x + 1]) {
case 0: score = screenData[x + 2]; break;
default: console.warn(`Unknown segment display value #${screenData[x + 1]} (value ${screenData[x + 2]})`); break;
}
expand(mapData, screenData[x + 1], true);
expand(mapData[screenData[x + 1]], screenData[x], false);
mapData[screenData[x + 1]][screenData[x]] = screenData[x + 2];
switch (screenData[x + 2]) {
case 3: paddleX = screenData[x]; break;
case 4: ball = { X: screenData[x], Y: screenData[x + 1] }; break;
}
}
mapData.reduce((r, i) => r.concat(i), []).forEach(i => counts[i]++);
let mapGraph = mapData.map(i => i.join('')).join('\n')
.replace(/0/g, ' ')
.replace(/1/g, '#')
.replace(/2/g, ':')
.replace(/3/g, '=')
.replace(/4/g, 'o');
return { mapData, counts, mapGraph, paddleX, ball, score };
}
let bigData = `1,380,379,385,1008,2655,793190,381,1005,381,12,99,109,2656,1101,0,0,383,1102,0,1,382,20101,0,382,1,20102,1,383,2,21102,37,1,0,1105,1,578,4,382,4,383,204,1,1001,382,1,382,1007,382,42,381,1005,381,22,1001,383,1,383,1007,383,24,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1101,0,-1,384,1106,0,119,1007,392,40,381,1006,381,161,1102,1,1,384,21001,392,0,1,21101,0,22,2,21102,0,1,3,21102,138,1,0,1105,1,549,1,392,384,392,20102,1,392,1,21101,22,0,2,21102,3,1,3,21101,161,0,0,1105,1,549,1102,0,1,384,20001,388,390,1,21002,389,1,2,21101,180,0,0,1106,0,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21001,389,0,2,21101,0,205,0,1105,1,393,1002,390,-1,390,1102,1,1,384,21002,388,1,1,20001,389,391,2,21101,0,228,0,1106,0,578,1206,1,261,1208,1,2,381,1006,381,253,21001,388,0,1,20001,389,391,2,21101,253,0,0,1105,1,393,1002,391,-1,391,1102,1,1,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,279,0,0,1105,1,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21102,304,1,0,1106,0,393,1002,390,-1,390,1002,391,-1,391,1102,1,1,384,1005,384,161,20101,0,388,1,20102,1,389,2,21101,0,0,3,21101,338,0,0,1105,1,549,1,388,390,388,1,389,391,389,20102,1,388,1,21002,389,1,2,21102,4,1,3,21102,365,1,0,1106,0,549,1007,389,23,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,355,19,19,1,1,21,109,3,21201,-2,0,1,21202,-1,1,2,21102,1,0,3,21102,414,1,0,1106,0,549,21201,-2,0,1,21201,-1,0,2,21101,0,429,0,1106,0,601,2102,1,1,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2105,1,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,21201,-3,0,-7,109,-8,2106,0,0,109,4,1202,-2,42,566,201,-3,566,566,101,639,566,566,2102,1,-1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,42,593,201,-2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0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,73,91,79,27,84,58,31,29,97,58,15,10,67,88,80,10,1,3,95,62,30,42,69,5,42,17,47,33,88,95,98,31,66,63,22,32,40,11,95,47,11,12,83,74,32,5,17,1,81,84,29,88,4,93,37,69,94,69,22,89,34,7,76,9,54,53,82,57,43,22,70,80,92,76,55,81,53,37,21,23,49,23,85,23,80,97,59,9,62,94,3,29,87,98,33,7,60,88,29,58,24,39,2,35,20,94,21,90,42,60,85,92,51,17,67,7,90,84,59,79,10,32,29,44,2,88,86,3,57,66,11,25,11,70,95,8,32,24,8,32,80,57,96,89,85,88,2,51,46,80,24,19,41,81,62,4,98,92,33,32,50,27,75,16,90,57,89,77,82,72,67,35,34,29,59,14,74,26,60,37,88,87,50,35,12,27,73,66,42,16,84,84,15,64,46,4,9,73,85,42,79,10,49,88,78,41,69,71,92,32,35,13,84,71,82,18,68,97,45,37,87,20,28,20,10,2,30,26,21,97,56,91,60,38,12,71,73,55,71,24,59,79,37,62,12,75,65,72,19,23,69,47,86,55,66,78,87,29,59,6,63,7,32,84,80,74,41,60,58,4,70,61,5,82,11,45,79,56,7,48,62,4,46,88,83,31,96,97,18,91,68,38,31,24,64,3,14,1,85,10,52,97,92,81,36,1,2,27,28,56,75,32,88,46,98,49,22,80,83,93,41,12,35,70,81,57,53,92,20,49,76,77,8,50,93,35,81,15,22,46,14,54,53,48,74,32,19,91,39,61,61,39,55,57,50,64,27,33,84,1,81,89,70,24,28,28,60,30,16,28,66,96,51,38,86,23,29,49,5,95,61,7,20,41,31,84,59,21,21,49,62,9,30,87,83,65,89,29,61,3,13,14,9,71,87,78,48,7,84,69,62,57,43,82,31,76,4,79,46,91,11,6,30,87,34,10,2,50,66,5,77,87,57,43,84,84,79,75,42,10,14,3,32,21,24,68,75,12,36,64,18,91,14,91,80,52,50,1,50,37,74,41,11,1,93,79,50,94,51,37,52,21,84,57,73,44,2,77,81,54,48,82,76,63,14,50,1,87,94,89,89,81,84,47,72,37,94,95,62,76,49,47,40,43,86,16,17,59,8,13,31,89,65,80,29,70,40,34,33,64,42,68,75,58,69,23,49,68,16,43,78,54,66,49,17,14,90,7,56,7,23,13,50,70,17,83,9,57,11,28,55,11,14,5,47,4,9,94,17,64,98,83,89,58,91,74,62,77,62,2,11,84,61,23,60,37,40,73,30,35,8,24,28,56,7,50,81,31,21,41,87,10,56,26,86,66,77,43,69,80,47,74,36,13,29,14,83,68,88,96,52,93,62,82,62,89,74,35,70,68,75,69,35,22,50,10,8,18,18,2,54,75,64,20,65,81,62,60,98,19,40,97,6,57,21,63,43,80,39,25,56,46,70,42,83,9,42,5,64,56,16,79,39,23,12,9,47,3,96,47,32,43,91,12,6,98,47,91,34,17,5,93,91,22,32,47,39,1,11,75,73,15,36,46,41,26,98,43,46,49,20,60,43,13,71,92,58,62,19,17,30,71,97,43,14,62,27,71,48,50,96,80,13,13,39,98,38,80,55,37,13,96,88,15,37,26,84,12,80,46,85,36,50,86,76,63,2,85,52,14,28,71,32,66,95,22,54,56,33,22,97,57,30,20,60,86,34,65,21,47,38,38,53,81,79,45,65,65,28,89,48,9,13,41,3,62,20,61,47,65,11,11,18,76,93,80,72,74,61,7,5,35,13,64,86,56,4,51,13,63,94,79,12,27,59,94,73,59,36,32,19,10,24,29,65,67,2,67,87,26,64,28,5,60,95,3,97,70,98,57,68,51,68,57,96,56,78,75,32,89,67,52,46,93,16,82,92,75,5,35,50,2,84,57,32,24,71,58,60,49,56,15,74,28,73,61,12,4,38,46,86,92,26,68,98,20,76,94,28,14,53,68,15,38,5,81,21,24,33,11,78,83,19,3,80,98,57,38,97,11,9,45,29,33,34,7,47,32,28,14,65,66,84,12,47,59,17,82,11,63,6,68,73,4,87,85,42,24,12,67,57,27,15,21,53,96,73,36,57,12,9,72,11,33,27,9,92,21,96,25,87,45,1,31,90,56,11,37,8,87,13,74,40,17,20,78,17,94,12,54,21,67,15,16,41,44,77,44,88,64,14,47,54,45,21,20,77,41,5,97,51,43,81,44,65,64,33,7,793190`;
let machine = new IntOpcodeMachine(bigData);
machine.setAt(0, 2);
let gameState = {
mapData: [],
paddleX: -1,
ball: { X: -1, Y: -1 },
score: 0
};
let cycles = 0;
while (!machine.run()) {
cycles++;
gameState = populateScreen(machine.getAllOutput(), gameState);
if (gameState.counts[2] === 0) break;
if (gameState.ball.X > gameState.paddleX) machine.addInput(1);
else if (gameState.ball.X < gameState.paddleX) machine.addInput(-1);
else machine.addInput(0);
}
console.log('Cycles:', ++cycles);
console.log('Final game state:', populateScreen(machine.getAllOutput(), gameState));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment