Day 17
This commit is contained in:
@@ -4,64 +4,66 @@ const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||
|
||||
const lines = input.split("\n");
|
||||
|
||||
let registerA = parseInt(lines[0].split(' ')[2]);
|
||||
let registerB = parseInt(lines[1].split(' ')[2]);
|
||||
let registerC = parseInt(lines[2].split(' ')[2]);
|
||||
let registerA = BigInt(parseInt(lines[0].split(' ')[2]));
|
||||
let registerB = BigInt(parseInt(lines[1].split(' ')[2]));
|
||||
let registerC = BigInt(parseInt(lines[2].split(' ')[2]));
|
||||
|
||||
let output: Array<number> = [];
|
||||
let output: Array<string> = [];
|
||||
|
||||
const instructions = lines[4].split(' ')[1].split(',').map((i: string) => parseInt(i));
|
||||
let instructionsStr = lines[4].split(' ')[1];
|
||||
const instructions = instructionsStr.split(',').map((i: string) => parseInt(i));
|
||||
|
||||
let pointer = 0;
|
||||
|
||||
function combo(op: number): number {
|
||||
if (op <= 3) {
|
||||
function combo(op: bigint): bigint {
|
||||
if (op <= 3n) {
|
||||
return op;
|
||||
}
|
||||
if (op === 4) {
|
||||
if (op === 4n) {
|
||||
return registerA;
|
||||
}
|
||||
if (op === 5) {
|
||||
if (op === 5n) {
|
||||
return registerB;
|
||||
}
|
||||
if (op === 6) {
|
||||
if (op === 6n) {
|
||||
return registerC;
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
function call(ins: number, op: number) {
|
||||
let bop = BigInt(op);
|
||||
if (ins === 0) {
|
||||
op = combo(op);
|
||||
registerA = Math.round(registerA / (2 ** op));
|
||||
bop = combo(bop);
|
||||
registerA = registerA >> bop;
|
||||
return;
|
||||
}
|
||||
if (ins === 1) {
|
||||
registerB = registerB ^ op;
|
||||
registerB ^= bop;
|
||||
return;
|
||||
}
|
||||
if (ins === 2) {
|
||||
op = combo(op);
|
||||
registerB = op % 8;
|
||||
bop = combo(bop);
|
||||
registerB = bop & 7n;
|
||||
return;
|
||||
}
|
||||
if (ins === 4) {
|
||||
registerB = registerB ^ registerC;
|
||||
registerB ^= registerC;
|
||||
return;
|
||||
}
|
||||
if (ins === 5) {
|
||||
op = combo(op);
|
||||
output.push(op % 8);
|
||||
bop = combo(bop);
|
||||
output.push(''+(bop & 7n));
|
||||
return;
|
||||
}
|
||||
if (ins === 6) {
|
||||
op = combo(op);
|
||||
registerB = Math.round(registerA / (2 ** op));
|
||||
bop = combo(bop);
|
||||
registerB = registerA >> bop;
|
||||
return;
|
||||
}
|
||||
if (ins === 7) {
|
||||
op = combo(op);
|
||||
registerC = Math.round(registerA / (2 ** op));
|
||||
bop = combo(bop);
|
||||
registerC = registerA >> bop;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -76,24 +78,23 @@ ins: ${typeof instructions[pointer] !== 'undefined' ? instructions[pointer] : 'N
|
||||
op: ${typeof instructions[pointer+1] !== 'undefined' ? instructions[pointer+1] : 'NA'}
|
||||
combo: ${typeof instructions[pointer+1] !== 'undefined' ? combo(instructions[pointer+1]) : 'NA'}
|
||||
|
||||
Output: ${output.join(',')}
|
||||
Output: ${output}
|
||||
`);
|
||||
}
|
||||
|
||||
function run() {
|
||||
while (pointer < instructions.length) {
|
||||
printPC();
|
||||
const ins = instructions[pointer];
|
||||
const op = instructions[pointer + 1];
|
||||
|
||||
if (ins === 3) {
|
||||
pointer = registerA === 0 ? pointer + 2 : op;
|
||||
pointer = registerA === 0n ? pointer + 2 : op;
|
||||
} else {
|
||||
call(ins, op);
|
||||
pointer += 2;
|
||||
}
|
||||
}
|
||||
printPC();
|
||||
}
|
||||
|
||||
run();
|
||||
console.log(output.join(','));
|
||||
Reference in New Issue
Block a user