All files / day6 puzzle2.js

21.05% Statements 4/19
0% Branches 0/2
0% Functions 0/3
25% Lines 4/16

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 261x   1x         1x                                 1x  
const { getPerformanceMade, extractData } = require('./puzzle1')
 
const extractAndJoinData = (fileInput) => {
  const [time, distance] = extractData(fileInput)
  return [[parseInt(time.join(''))], [parseInt(distance.join(''))]]
}
 
const resolvePuzzle = (inputFile) => {
  const possibilities = []
  const [time, distance] = extractAndJoinData(inputFile)
 
  for (let i = 0; i < time.length; i++) {
    let waysToWin = 0
    for (let j = 0; j < time[i]; j++) {
      const distanceMade = getPerformanceMade(j, time[i])
      if (distanceMade > distance[i]) {
        waysToWin++
      }
    }
    possibilities.push(waysToWin)
  }
  return possibilities.reduce((prev, curr) => prev * curr)
}
 
module.exports = { resolvePuzzle }