From 5b1c11eb55477bd6ff16e0d91b61a6c364f08dc7 Mon Sep 17 00:00:00 2001 From: Skye Ewers Date: Sat, 2 Dec 2023 06:34:31 +0100 Subject: [PATCH] Day 02 Part 02 --- 02/index.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/02/index.ts b/02/index.ts index ca3ec8b..331a430 100644 --- a/02/index.ts +++ b/02/index.ts @@ -14,7 +14,7 @@ let limits: {[id: string]: Number} = { } console.log("Solution first part: " + partOne(input)) -console.log("Solution second part: " + partTwo(testInput)) +console.log("Solution second part: " + partTwo(input)) function partOne(input: string): string { @@ -49,4 +49,24 @@ function partOne(input: string): string { } function partTwo(input: string): string { + let powers = [] + for (const game of input.split('\n')) { + let colorMaximums: {[id: string]: Number} = {blue: 0, red: 0, green: 0} + + const gameParts = game.split(":") + const name = gameParts[0] + const moves = gameParts[1] + for (const round of moves.split(';')) { + for (const stat of round.split(',')) { + let statParts = stat.split(' ') + let number = parseInt(statParts[1]) + let color = statParts[2] + if (colorMaximums[color] < number) { + colorMaximums[color] = number + } + } + } + powers.push(colorMaximums['red'] * colorMaximums['green'] * colorMaximums['blue']) + } + return `${powers.reduce((acc, item) => { return item += acc })}` } \ No newline at end of file