Day 02 Part 02
This commit is contained in:
parent
5b89f6fb78
commit
5b1c11eb55
1 changed files with 21 additions and 1 deletions
22
02/index.ts
22
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 })}`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue