72 lines
No EOL
2.2 KiB
TypeScript
72 lines
No EOL
2.2 KiB
TypeScript
const fs = require('fs').promises;
|
|
|
|
const input: string = (await fs.readFile('./input.txt')).toString()
|
|
const testInput = `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`
|
|
|
|
let limits: {[id: string]: Number} = {
|
|
red: 12,
|
|
green: 13,
|
|
blue: 14
|
|
}
|
|
|
|
console.log("Solution first part: " + partOne(input))
|
|
console.log("Solution second part: " + partTwo(input))
|
|
|
|
|
|
function partOne(input: string): string {
|
|
let possibleGames = []
|
|
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
|
|
}
|
|
}
|
|
}
|
|
let gamePossible = true
|
|
for (const [colorName, maximum] of Object.entries(limits)) {
|
|
if (colorMaximums[colorName] > maximum) {
|
|
gamePossible = false
|
|
}
|
|
}
|
|
if (gamePossible) {
|
|
possibleGames.push(parseInt(name.split(' ')[1]))
|
|
}
|
|
}
|
|
return `${possibleGames.reduce((acc, item) => { return item += acc })}`
|
|
}
|
|
|
|
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 })}`
|
|
} |