Skip to content

Instantly share code, notes, and snippets.

@Rustywolf
Created December 23, 2019 05:22
Show Gist options
  • Save Rustywolf/aeca93ac92c92597dd891545cd32d6fd to your computer and use it in GitHub Desktop.
Save Rustywolf/aeca93ac92c92597dd891545cd32d6fd to your computer and use it in GitHub Desktop.
// allMatches(string, regex)
// md5(string)
function main(input) {
let mc = [];
for (let x = 0; x < 50; x++) {
let int = new Intcode(input.split(/,/g).map(x => Number(x)));
int.input = [x];
mc.push(int)
}
let seen = {};
let nat = {
x: -1,
y: -1
};
while (true) {
for (let x = 0; x < 50; x++) {
let int = mc[x];
if (int.waiting) {
int.input = [-1];
}
let res = int.tick();
if (int.output.length == 3) {
let addr = int.output[0];
let dx = int.output[1];
let dy = int.output[2];
if (addr == 255) {
nat.x = dx;
nat.y = dy;
} else {
mc[addr].input.push(dx, dy);
}
int.output = [];
}
}
let idle = true;
for (let x = 0; x < 50; x++) {
if (mc[x].input.length !== 0 || !mc[x].receiving) idle = false;
}
if (idle) {
console.log(nat.y);
if (seen[nat.y]) {
return nat.y;
}
seen[nat.y] = true;
mc[0].input.push(nat.x, nat.y);
}
}
}
const Intcode = require('../intcode/intcode');
const fs = require('fs');
const clipboardy = require('clipboardy');
const md5 = require('md5');
function allMatches(input, regex) {
var result;
var results = [];
while ((result = regex.exec(input)) !== null) {
results.push(result);
}
return results;
}
Array.prototype.sum = function () {
return this.reduce((t, c) => t + Number(c), 0);
}
fs.readFile("./input.txt", async function (err, data) {
if (err) {
console.log(err);
} else {
let out = main(String(data));
console.log(out);
clipboardy.writeSync(String(out));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment