diff options
Diffstat (limited to 'day-4/script.ts')
-rw-r--r-- | day-4/script.ts | 13 |
1 files changed, 10 insertions, 3 deletions
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 }); |