Day 02 Part 02

This commit is contained in:
Skye Ewers 2023-12-02 06:34:31 +01:00
parent 5b89f6fb78
commit 5b1c11eb55

View file

@ -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 })}`
}