2024-12-17 16:52:44 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
|
|
|
|
|
|
|
|
|
const lines = input.split("\n");
|
|
|
|
|
|
2024-12-17 22:12:31 +00:00
|
|
|
let registerA = BigInt(parseInt(lines[0].split(' ')[2]));
|
|
|
|
|
let registerB = BigInt(parseInt(lines[1].split(' ')[2]));
|
|
|
|
|
let registerC = BigInt(parseInt(lines[2].split(' ')[2]));
|
2024-12-17 16:52:44 +00:00
|
|
|
|
2024-12-17 22:12:31 +00:00
|
|
|
let output: Array<string> = [];
|
2024-12-17 16:52:44 +00:00
|
|
|
|
2024-12-17 22:12:31 +00:00
|
|
|
let instructionsStr = lines[4].split(' ')[1];
|
|
|
|
|
const instructions = instructionsStr.split(',').map((i: string) => parseInt(i));
|
2024-12-17 16:52:44 +00:00
|
|
|
|
|
|
|
|
let pointer = 0;
|
|
|
|
|
|
2024-12-17 22:12:31 +00:00
|
|
|
function combo(op: bigint): bigint {
|
|
|
|
|
if (op <= 3n) {
|
2024-12-17 16:52:44 +00:00
|
|
|
return op;
|
|
|
|
|
}
|
2024-12-17 22:12:31 +00:00
|
|
|
if (op === 4n) {
|
2024-12-17 16:52:44 +00:00
|
|
|
return registerA;
|
|
|
|
|
}
|
2024-12-17 22:12:31 +00:00
|
|
|
if (op === 5n) {
|
2024-12-17 16:52:44 +00:00
|
|
|
return registerB;
|
|
|
|
|
}
|
2024-12-17 22:12:31 +00:00
|
|
|
if (op === 6n) {
|
2024-12-17 16:52:44 +00:00
|
|
|
return registerC;
|
|
|
|
|
}
|
|
|
|
|
return op;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function call(ins: number, op: number) {
|
2024-12-17 22:12:31 +00:00
|
|
|
let bop = BigInt(op);
|
2024-12-17 16:52:44 +00:00
|
|
|
if (ins === 0) {
|
2024-12-17 22:12:31 +00:00
|
|
|
bop = combo(bop);
|
|
|
|
|
registerA = registerA >> bop;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 1) {
|
2024-12-17 22:12:31 +00:00
|
|
|
registerB ^= bop;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 2) {
|
2024-12-17 22:12:31 +00:00
|
|
|
bop = combo(bop);
|
|
|
|
|
registerB = bop & 7n;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 4) {
|
2024-12-17 22:12:31 +00:00
|
|
|
registerB ^= registerC;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 5) {
|
2024-12-17 22:12:31 +00:00
|
|
|
bop = combo(bop);
|
|
|
|
|
output.push(''+(bop & 7n));
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 6) {
|
2024-12-17 22:12:31 +00:00
|
|
|
bop = combo(bop);
|
|
|
|
|
registerB = registerA >> bop;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ins === 7) {
|
2024-12-17 22:12:31 +00:00
|
|
|
bop = combo(bop);
|
|
|
|
|
registerC = registerA >> bop;
|
2024-12-17 16:52:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printPC() {
|
|
|
|
|
console.log(`
|
|
|
|
|
A: ${registerA}
|
|
|
|
|
B: ${registerB}
|
|
|
|
|
C: ${registerC}
|
|
|
|
|
|
|
|
|
|
ins: ${typeof instructions[pointer] !== 'undefined' ? instructions[pointer] : 'NA'}
|
|
|
|
|
op: ${typeof instructions[pointer+1] !== 'undefined' ? instructions[pointer+1] : 'NA'}
|
|
|
|
|
combo: ${typeof instructions[pointer+1] !== 'undefined' ? combo(instructions[pointer+1]) : 'NA'}
|
|
|
|
|
|
2024-12-17 22:12:31 +00:00
|
|
|
Output: ${output}
|
2024-12-17 16:52:44 +00:00
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
|
while (pointer < instructions.length) {
|
|
|
|
|
const ins = instructions[pointer];
|
|
|
|
|
const op = instructions[pointer + 1];
|
|
|
|
|
|
|
|
|
|
if (ins === 3) {
|
2024-12-17 22:12:31 +00:00
|
|
|
pointer = registerA === 0n ? pointer + 2 : op;
|
2024-12-17 16:52:44 +00:00
|
|
|
} else {
|
|
|
|
|
call(ins, op);
|
|
|
|
|
pointer += 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run();
|
2024-12-17 22:12:31 +00:00
|
|
|
console.log(output.join(','));
|