summaryrefslogtreecommitdiff
path: root/day-2
diff options
context:
space:
mode:
Diffstat (limited to 'day-2')
-rw-r--r--day-2/script.ts21
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 });