From 60740f6e984066f47130b5fdd849eedfeb26bf94 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 2 Dec 2023 17:28:37 +0300 Subject: feat(day-2): add part 2 solution --- day-2/script.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'day-2/script.ts') 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 }); -- cgit v1.2.3