advent-of-code/2019/02/solution1.js

36 lines
842 B
JavaScript
Raw Normal View History

2020-11-30 17:25:40 +01:00
const fs = require("fs");
const inputFileName = "input";
async function main() {
const input = await fs.promises.readFile(inputFileName, "utf-8");
const machine = input.split(",").map((code) => parseInt(code));
machine[1] = 12;
machine[2] = 2;
console.log(machine);
let curIdx = 0;
let opCode = machine[curIdx];
while (opCode !== 99) {
const val1 = machine[machine[curIdx + 1]];
const val2 = machine[machine[curIdx + 2]];
const register = machine[curIdx + 3];
console.log(opCode, val1, val2, register);
if (opCode === 1) {
machine[register] = val1 + val2;
} else if (opCode === 2) {
machine[register] = val1 * val2;
} else {
throw Error("invalid opcode");
}
curIdx += 4;
opCode = machine[curIdx];
}
console.log(machine);
console.log(machine[0]);
}
main();