Skip to content

Commit 69d3e3e

Browse files
committed
refactor(2025/day/10/part2): @macil/z3-solver
drops delegating to node in favor of a patched workaround for Bun/Deno denoland/deno#17171
1 parent e62a1bb commit 69d3e3e

4 files changed

Lines changed: 27 additions & 26 deletions

File tree

2025/day/10/part/2/solve.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
/** delegates to Node.js until https://github.com/denoland/deno/issues/17171 is fixed */
21
export default async function solve(input: string) {
3-
const command = new Deno.Command("node", {
4-
args: [`${import.meta.dirname}/solve.mts`],
5-
stdin: "piped",
6-
stdout: "piped",
7-
});
8-
9-
const child = command.spawn();
10-
const writer = child.stdin.getWriter();
11-
await writer.write(new TextEncoder().encode(input));
12-
writer.releaseLock();
13-
await child.stdin.close();
14-
15-
const { stdout } = await child.output();
16-
return +new TextDecoder().decode(stdout);
2+
const { href: workerUrl } = new URL("./worker.ts", import.meta.url);
3+
const worker = new Worker(workerUrl, { type: "module" });
4+
try {
5+
const { promise, resolve } = Promise.withResolvers<number>();
6+
worker.onmessage = ({ data }) => resolve(data);
7+
worker.postMessage(input);
8+
return await promise;
9+
} finally {
10+
worker.terminate();
11+
}
1712
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import process from "node:process";
2-
import { text } from "node:stream/consumers";
1+
/// <reference no-default-lib="true" />
2+
/// <reference lib="deno.worker" />
3+
34
import { init } from "z3-solver";
45
import { parseManual } from "../../manuals.ts";
56

6-
export default async function solve(input: string) {
7+
async function solve(input: string) {
78
const manual = parseManual(input);
89
const { Context } = await init();
10+
const { Int, Optimize } = Context("main");
911
let sum = 0;
1012
for (const { buttons, requirements } of manual) {
11-
const { Int, Optimize } = Context("main");
1213
const variables = buttons.map((_, i) => Int.const(`b${i}`));
1314
const optimize = new Optimize();
1415
optimize.add(
@@ -29,6 +30,4 @@ export default async function solve(input: string) {
2930
return sum;
3031
}
3132

32-
const input = await text(process.stdin);
33-
const output = await solve(input);
34-
console.log(output);
33+
self.onmessage = async ({ data }) => self.postMessage(await solve(data));

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"@std/path": "jsr:@std/path@^1.0.8",
1818
"nerdamer": "npm:nerdamer@^1.1.13",
1919
"pretty-ms": "npm:pretty-ms@^9.3.0",
20-
"z3-solver": "npm:z3-solver@^4.15.4"
20+
"z3-solver": "jsr:@macil/z3-solver@^1.0.4"
2121
}
2222
}

deno.lock

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)