diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-12-02 17:28:37 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-12-02 17:28:37 +0300 |
commit | 60740f6e984066f47130b5fdd849eedfeb26bf94 (patch) | |
tree | 7a8dd4022e597b70282ee4b5b744d0f22d149e28 | |
parent | 5406a3a52036b95726f368d0b22f8c73da48f9a0 (diff) | |
download | aoc-2023-60740f6e984066f47130b5fdd849eedfeb26bf94.tar.gz |
feat(day-2): add part 2 solution
-rw-r--r-- | day-2/script.ts | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/day-2/script.ts b/day-2/script.ts index 4e29bbb..29c754f 100644 --- a/day-2/script.ts +++ b/day-2/script.ts @@ -26,6 +26,23 @@ function isPossible( }, true); } +function getMinimalPossibleDraw(draws: Draw[]) { + return COLORS.reduce( + (acc, color) => { + acc[color] = draws.reduce((max, draw) => { + return Math.max(draw[color], max); + }, 0); + + return acc; + }, + { red: 0, green: 0, blue: 0 }, + ); +} + +function power(draw: Draw) { + return Object.values(draw).reduce((product, value) => product * value, 1); +} + const result = input .split("\n") .slice(0, -1) @@ -46,7 +63,7 @@ const result = input }); return { id, draws }; }) - .filter((game) => isPossible(game)) - .reduce((acc, game) => acc + game.id, 0); + .map((game) => getMinimalPossibleDraw(game.draws)) + .reduce((acc, draw) => acc + power(draw), 0); console.log({ result }); |