Skip to content

Instantly share code, notes, and snippets.

@potch
Created December 19, 2019 06:48
Show Gist options
  • Save potch/885b4561736aa1e77412bcc9fa51dc3c to your computer and use it in GitHub Desktop.
Save potch/885b4561736aa1e77412bcc9fa51dc3c to your computer and use it in GitHub Desktop.
you really got a hold on me
const fs = require('fs');
const { IntCode, test } = require('../IntCode.js');
class Grid {
constructor({data, width, height}) {
if (data) {
this.data = data;
this.width = data[0].length;
this.height = data.length;
}
if (width && height) {
this.width = width;
this.height = height;
this.data = [];
for (let i = 0; i < height; i++) {
this.data.push(Array(width));
}
}
}
get(x, y) {
return this.data[y][x];
}
set(x, y, v) {
this.data[y][x] = v;
}
forEach(fn) {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let r = fn(x, y, this.get(x, y));
if (r) return;
}
}
}
is(x, y, v) {
return this.get(x, y) === v;
}
toString() {
return this.data.map(row => row.join('')).join('\n');
}
static fromString(s) {
return new Grid(s.split('\n').map(row => row.split('')));
}
}
async function day19 () {
let input = fs.readFileSync(__dirname + '/input.txt').toString('utf8');
let code = input.split(',').map(n => parseInt(n));
let g = new Grid({width: 50, height:50});
let pts = [];
let count = 0;
let cpu = new IntCode(code);
function test(x, y) {
cpu.reset();
cpu.input(x, y);
let o = cpu.run({untilOutput:true});
return o;
}
console.log('performing initial scan');
// g.forEach((x, y) => g.set(x, y, '.#'[test(x, y)]));
let found = false;
let y = 10;
let x = 0;
let ls = 0;
let step = 2000;
while (!found) {
while (!test(x, y)) {
x++;
}
let s = 0;
let ty = y;
let tx = x;
while (test(tx, ty)) {
tx++;
ty--;
s++;
}
console.log(x, y, tx, ty, step, s);
if (s === 100 && ls < s && step === 1) {
found = true;
console.log(x, ty + 1);
}
if (s >= 100) {
y -= step;
} else {
y += step;
}
step = Math.ceil(step / 2);
x = Math.floor(y * .75);
ls = s;
}
}
day19().catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment