I remembered Array.shift

This commit is contained in:
Daniel Flanagan 2021-12-01 11:39:38 -06:00
parent 8afaa22a08
commit e32d61749d
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -34,17 +34,13 @@ export async function part2(
windowSize = 3, windowSize = 3,
): Promise<number> { ): Promise<number> {
let increases = 0; let increases = 0;
// TODO: use a proper FIFO queue data structure for this? const window: number[] = await Promise.all(Array.from(
let window: number[] = await Promise.all(Array.from(
Array(windowSize), Array(windowSize),
async (_x, _i) => parseInt((await input.next()).value), async (_x, _i) => parseInt((await input.next()).value),
)); ));
for await (const depth of input) { for await (const depth of input) {
if (depth > window[0]) increases++; if (depth > (window.shift() as number)) increases++;
// TODO: if this was a proper queue, we could just push it on the queue and
// expect the first-in value to fall out
window = window.slice(1);
window.push(depth); window.push(depth);
} }
return increases; return increases;