diff --git a/2024/17/index.ts b/2024/17/index.ts index 468c09e..756896f 100644 --- a/2024/17/index.ts +++ b/2024/17/index.ts @@ -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 = []; +let output: Array = []; -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(',')); \ No newline at end of file diff --git a/2024/17/index2.ts b/2024/17/index2.ts new file mode 100644 index 0000000..9ad77fd --- /dev/null +++ b/2024/17/index2.ts @@ -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 +*/ \ No newline at end of file