Solve 2019/02
parent
468f14fe72
commit
ac36ac8d93
@ -0,0 +1 @@
|
|||||||
|
1,9,10,3,2,3,11,0,99,30,40,50
|
@ -0,0 +1 @@
|
|||||||
|
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,2,9,19,23,2,23,10,27,1,6,27,31,1,31,6,35,2,35,10,39,1,39,5,43,2,6,43,47,2,47,10,51,1,51,6,55,1,55,6,59,1,9,59,63,1,63,9,67,1,67,6,71,2,71,13,75,1,75,5,79,1,79,9,83,2,6,83,87,1,87,5,91,2,6,91,95,1,95,9,99,2,6,99,103,1,5,103,107,1,6,107,111,1,111,10,115,2,115,13,119,1,119,6,123,1,123,2,127,1,127,5,0,99,2,14,0,0
|
@ -0,0 +1,35 @@
|
|||||||
|
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();
|
@ -0,0 +1,45 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
for (let noun = 0; noun < 100; noun++) {
|
||||||
|
for (let verb = 0; verb < 100; verb++) {
|
||||||
|
machine[1] = noun;
|
||||||
|
machine[2] = verb;
|
||||||
|
if (intMachine(machine.slice()) == 19690720) {
|
||||||
|
console.log(100 * noun + verb);
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function intMachine(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];
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
return machine[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
Loading…
Reference in New Issue