Skip to content

Commit

Permalink
Add Threading
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 5, 2023
1 parent a135287 commit 88b28cd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions clients/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const runner = new Runner();

function startWebsocket() {
const ws = new WebSocket(Deno.env.get("WEBSOCKETURL") ?? 'ws://localhost:1337/');
ws.addEventListener('message', (data) => {
ws.addEventListener('message', async (data) => {
const message = data.data;
const split = message.split(':');
if (split[0] !== 'aocserver') return;
Expand All @@ -17,7 +17,7 @@ function startWebsocket() {
const data = split.slice(4).join(':');
const solution = split[3];
console.log(`running ${solution} with runid ${runid}`);
const result = runner.run(solution, data);
const result = await runner.run(solution, data);
ws.send(`aocclient:ts:result:${runid}:[${result[0]}, ${result[1]}]`);
}
});
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export class Runner {
async loadSolutions() {
this.list = (await Promise.all(Array.from(Deno.readDirSync('./solutions')).filter(file => file.name.startsWith('S')).map(async file => [file.name.replace('.ts', ''), new (await import('./solutions/' + file.name)).default()] as [string, ISolution])));
}
run(name: string, input: string) {
async run(name: string, input: string) {
const solution = this.list.find(s => s[0] === name);
if (solution) {
return [solution[1].firstPart(input), solution[1].secondPart(input)];
return [await solution[1].firstPart(input), await solution[1].secondPart(input)];
}
return [0, 0];
}
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/solutions/ISolution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface ISolution {
firstPart(input: string): number | string;
secondPart(input: string): number | string;
firstPart(input: string): (number | string) | Promise<number | string>;
secondPart(input: string): (number | string) | Promise<number | string>;
}

export default ISolution;
43 changes: 31 additions & 12 deletions clients/typescript/solutions/S2305.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ISolution from "./ISolution.ts";
import Thread from "https://deno.land/x/Thread@v4.1.0/Thread.ts";

export default class S2303 implements ISolution {
firstPart(input: string): string | number {
Expand All @@ -15,24 +16,42 @@ export default class S2303 implements ISolution {
}
return Math.min(...data);
}
secondPart(input: string): string | number {
async secondPart(input: string): Promise<string | number> {
const out = input.replace('seeds: ', '').split('\n\n').map(g => g.split('\n').filter(s => !s.includes(':')).map(s => s.split(' ').map(Number)));
let lowest = Number.MAX_SAFE_INTEGER;
const promises = [];
for (let i = 0; i < out[0][0].length / 2; i++) {
for (let j = 0; j < out[0][0][i * 2 + 1]; j++) {
let number = out[0][0][i * 2] + j;
for (let h = 1; h < out.length; h++) {
const rule = out[h].find(h => h[1] <= number && h[1] + h[2] > number)
if (rule) {
number = number - rule[1] + rule[0]
const worker = new Thread<number>((e) => {
const { out, i } = e.data as { out: number[][][], i: number };
let lowest = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < out[0][0][i * 2 + 1]; j++) {
let number = out[0][0][i * 2] + j;
for (let h = 1; h < out.length; h++) {
const rule = out[h].find(h => h[1] <= number && h[1] + h[2] > number)
if (rule) {
number = number - rule[1] + rule[0]
}
}
if (number < lowest) {
lowest = number;
}
}
if (number < lowest) {
lowest = number;
console.log('New low:'+lowest);
}
}
return lowest;
});
worker.postMessage({
out, i
});
const promise = new Promise<void>((resolve) => {
worker.onMessage((e) => {
if (e < lowest) {
lowest = e;
}
resolve();
})
});
promises.push(promise);
}
await Promise.all(promises);
return lowest;
}

Expand Down

0 comments on commit 88b28cd

Please sign in to comment.