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