All files / day2 puzzle2.js

100% Statements 19/19
100% Branches 2/2
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x   1x 111x 111x 111x 476x 1204x 575x       111x     1x 2x 2x 2x 105x 105x     103x   1x  
const { readFile } = require('../helpers/file')
const { extractGameResults } = require('./puzzle1')
 
const getMinimumRequiredBalls = (line) => {
  const gameResults = extractGameResults(line)
  const minimumConfiguration = { blue: 0, green: 0, red: 0 }
  for (const result of gameResults) {
    Object.entries(result).forEach(([key, value]) => {
      if (minimumConfiguration[key] < value) {
        minimumConfiguration[key] = value
      }
    })
  }
  return minimumConfiguration
}
 
const resolvePuzzle = (fileInput) => {
  const puzzle = readFile(fileInput)
  const pows = []
  for (const line of puzzle) {
    const minimumValues = getMinimumRequiredBalls(line)
    pows.push(minimumValues.green * minimumValues.blue * minimumValues.red)
  }
  // Calculate the sum of ids
  return pows.reduce((accumulator, currentValue) => accumulator + currentValue)
}
module.exports = { getMinimumRequiredBalls, resolvePuzzle }