this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

split up seed ranges into promises, it worked!

+25 -15
+23 -13
2023/day05/part2.ts
··· 6 6 new TextDecoder().decode(bytes), 7 7 ) 8 8 ).trim(); 9 - console.log(answer(input)); 9 + console.log(await answer(input)); 10 10 } 11 11 12 12 type Range = { ··· 21 21 ranges: Range[]; 22 22 }; 23 23 24 - export function answer(input: string): number { 24 + export async function answer(input: string): Promise<number> { 25 25 const [seedLine, _, ...mapLines] = input.split("\n"); 26 - const seedRanges = seedLine 26 + const seedRangeLines = seedLine 27 27 .replace("seeds: ", "") 28 28 .split(" ") 29 29 .map((x) => parseInt(x)); ··· 44 44 return { from, to, ranges }; 45 45 }); 46 46 47 - let minLocNum = Infinity; 48 - for (let i = 0; i < seedRanges.length; i += 2) { 49 - const from = seedRanges[i]; 50 - const to = from + seedRanges[i + 1]; 51 - for (let j = 0; j < to - from; j++) { 52 - const seed = seedRanges[i] + j; 53 - const locNum = getLocationNumber(seed, mapRanges); 54 - if (locNum < minLocNum) minLocNum = locNum; 55 - } 47 + const seedRanges: [number, number][] = []; 48 + for (let i = 0; i < seedRangeLines.length; i += 2) { 49 + const from = seedRangeLines[i]; 50 + const to = from + seedRangeLines[i + 1]; 51 + seedRanges.push([from, to]); 56 52 } 53 + const rangeMins = await Promise.all( 54 + seedRanges.map( 55 + ([from, to]) => 56 + new Promise<number>((resolve) => { 57 + let minLocNum = Infinity; 58 + for (let j = 0; j < to - from; j++) { 59 + const seed = from + j; 60 + const locNum = getLocationNumber(seed, mapRanges); 61 + if (locNum < minLocNum) minLocNum = locNum; 62 + } 63 + return resolve(minLocNum); 64 + }), 65 + ), 66 + ); 57 67 58 - return minLocNum; 68 + return Math.min(...rangeMins); 59 69 } 60 70 61 71 function getLocationNumber(seed: number, mapRanges: MapRanges[]): number {
+2 -2
2023/day05/test.ts
··· 42 42 assertEquals(p1.answer(examples), 35); 43 43 }); 44 44 45 - Deno.test("part2", () => { 45 + Deno.test("part2", async () => { 46 46 const examples = [ 47 47 "seeds: 79 14 55 13", 48 48 "", ··· 79 79 "60 56 37", 80 80 "56 93 4", 81 81 ].join("\n"); 82 - assertEquals(p2.answer(examples), 46); 82 + assertEquals(await p2.answer(examples), 46); 83 83 });