From 3d6836a790e1cdd662a323a3eb5b19a6e6e19970 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 4 Dec 2023 15:36:44 +0300 Subject: feat(day-4): add part 2 solution --- day-4/script.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'day-4/script.ts') diff --git a/day-4/script.ts b/day-4/script.ts index 53488b5..cb2f643 100644 --- a/day-4/script.ts +++ b/day-4/script.ts @@ -14,17 +14,24 @@ function parseNumbers(s: string) { return chunks(s.split(""), 3).map((chunk) => Number(chunk.join(""))); } -const result = fs +const lines = fs .readFileSync("./input.txt") .toString() .split("\n") - .slice(0, -1) + .slice(0, -1); + +const result = lines .map((line) => { const [_cardStr, dataStr] = line.split(": "); const [winning, existing] = dataStr.split("| ").map(parseNumbers); return { winning, existing }; }) .map((data) => data.existing.filter((x) => data.winning.includes(x)).length) - .reduce((acc, winCount) => acc + (winCount ? 2 ** (winCount - 1) : 0), 0); + .reduce((acc, winCount, index) => { + return acc.map((v, i) => + i > index && i <= index + winCount ? v + acc[index] : v, + ); + }, new Array(lines.length).fill(1)) + .reduce((acc, v) => acc + v, 0); console.log({ result }); -- cgit v1.2.3