-
-
Save Rustywolf/c7b692fb03e53145282ba006265b03ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// allMatches(string, regex) | |
// md5(string) | |
function main(input) { | |
let int = new Intcode(input.split(/,/g).map(x => Number(x))); | |
int.input = []; | |
let x = 0; | |
let y = 0; | |
let map = {}; | |
let ox = 0; | |
let oy = 0; | |
let seen = {}; | |
let next = []; | |
let tick = 0; | |
function distance() { | |
let nextStack = []; | |
for (let node of next) { | |
let x = node.x; | |
let y = node.y; | |
seen[(y) * 1000 + x] = true; | |
for (let dir = 1; dir <= 4; dir++) { | |
let offsetX = (dir == 1 || dir == 2) ? 0 : dir == 3 ? -1 : 1; | |
let offsetY = (dir == 3 || dir == 4) ? 0 : dir == 1 ? -1 : 1; | |
if (!seen[(y + offsetY) * 1000 + offsetX + x] && map[(y + offsetY) * 1000 + x + offsetX] == 1) { | |
nextStack.push({ x: x + offsetX, y: y + offsetY }); | |
} | |
} | |
} | |
next = nextStack; | |
tick++; | |
} | |
let ax = 0; | |
function finish() { | |
renderMap(); | |
next = [{ x: ox, y: oy }]; | |
while (next.length > 0) { | |
distance(); | |
} | |
return tick; | |
} | |
let dir; | |
while (true) { | |
let res = int.tick(); | |
if (int.waiting) { | |
dir = Math.floor(Math.random() * 4) + 1; | |
int.input = [dir]; | |
ax++; | |
} | |
if (int.output.length > 0) { | |
let out = int.output.shift(); | |
let offsetX = (dir == 1 || dir == 2) ? 0 : dir == 3 ? -1 : 1; | |
let offsetY = (dir == 3 || dir == 4) ? 0 : dir == 1 ? -1 : 1; | |
map[(y + offsetY) * 1000 + (x + offsetX)] = out; | |
if (out == 1 || out == 2) { | |
y += offsetY; | |
x += offsetX; | |
} | |
if (out == 2) { | |
ox = x; | |
oy = y; | |
} | |
if (ax > 1000000) { | |
return finish(); | |
} | |
} | |
if (res !== undefined) { | |
console.log(int.output); | |
return res; | |
} | |
} | |
} | |
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