Skip to content

Commit 3665a7b

Browse files
committed
2024 day 17
1 parent 96d6ca1 commit 3665a7b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

2024/day17/lilcomputer.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const compute = (/** @type {bigint} */ regA) => {
2+
let out = [],
3+
a = regA,
4+
b,
5+
c;
6+
7+
while (a != 0n) {
8+
b = a & 7n;
9+
b ^= 6n; // Flips 4,2 bits
10+
c = a >> b;
11+
b ^= c;
12+
b ^= 7n; // Flips the bits
13+
a >>= 3n;
14+
out.push(b & 7n); // Output first 3 bits of b
15+
}
16+
17+
return out;
18+
};
19+
20+
const input = (await Bun.file("./input.txt").text()).split("\n");
21+
22+
const program = input[4].split(" ")[1].split(","),
23+
registerA = BigInt(input[0].split(" ")[2]);
24+
25+
const search = () => {
26+
let stack = [];
27+
28+
stack.push([]);
29+
30+
while (stack.length > 0) {
31+
const ans = stack.pop(),
32+
ansLen = ans.length;
33+
34+
const checkPart = (a) => a.slice(15 - ansLen).join(",");
35+
36+
// Goal reached
37+
if (ansLen == 16) return parseInt(ans.join(""), 8);
38+
39+
// Our found so far + whatever padding to 16
40+
const paddedInput = ans.concat(new Array(16 - ansLen).fill(1));
41+
42+
for (let i = 0; i < 8; i++) {
43+
const testResult = compute(
44+
BigInt(parseInt(paddedInput.with(ansLen, i).join(""), 8))
45+
);
46+
47+
// If the end matches the target
48+
if (
49+
testResult.length == 16 &&
50+
checkPart(testResult) == checkPart(program)
51+
)
52+
stack.push(ans.concat(i));
53+
}
54+
}
55+
};
56+
57+
console.log("With given values:", compute(registerA).join(","));
58+
console.log("Register A to get program output:", search());

0 commit comments

Comments
 (0)