diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-12-04 15:36:44 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-12-04 15:36:44 +0300 |
commit | 3d6836a790e1cdd662a323a3eb5b19a6e6e19970 (patch) | |
tree | 6c75ad12f7d828477d95d0fab853053aa53a99ec | |
parent | 78f94d5bde56dc30fbb845ebab56a7f0ebda8e14 (diff) | |
download | aoc-2023-3d6836a790e1cdd662a323a3eb5b19a6e6e19970.tar.gz |
feat(day-4): add part 2 solution
-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 }); |