Files
AoC/2024/17/index2.ts
2024-12-17 22:12:31 +00:00

42 lines
807 B
TypeScript

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
*/