Solve 2024-06 (using bruteforce)

This commit is contained in:
2024-12-06 21:05:01 +01:00
parent 8e8012e8a5
commit 62baa825f2
8 changed files with 220 additions and 1 deletions
+29
View File
@@ -0,0 +1,29 @@
import { availableParallelism } from 'os';
import pLimit from 'p-limit';
import { join } from 'path';
import { sumArray } from '../../utils/array.ts';
const workerFile = new URL('worker.ts', import.meta.url).href;
const eva = (i: number) =>
new Promise<number>((resolve) => {
const worker = new Worker(workerFile);
worker.postMessage(i);
worker.onmessage = (event) => {
console.log('row:', i, 'found', event.data);
resolve(event.data as number);
};
});
const input = await Bun.file(join(__dirname, 'input.txt')).text();
const rowCount = input.split('\n').length;
const work = new Array(rowCount).fill(0).map((_, i) => i);
const limit = pLimit(availableParallelism());
const solution = await Promise.all(work.map((w) => limit(eva, w)));
console.log(solution);
console.log(sumArray(solution));