From e32d61749dd64126005c5bf21353a5fa267f6b30 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Wed, 1 Dec 2021 11:39:38 -0600 Subject: [PATCH] I remembered Array.shift --- 2021/1.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/2021/1.ts b/2021/1.ts index b10ba7a..3e78035 100644 --- a/2021/1.ts +++ b/2021/1.ts @@ -34,17 +34,13 @@ export async function part2( windowSize = 3, ): Promise { let increases = 0; - // TODO: use a proper FIFO queue data structure for this? - let window: number[] = await Promise.all(Array.from( + const window: number[] = await Promise.all(Array.from( Array(windowSize), async (_x, _i) => parseInt((await input.next()).value), )); for await (const depth of input) { - if (depth > window[0]) 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); + if (depth > (window.shift() as number)) increases++; window.push(depth); } return increases;