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