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 28 29 30 31 32 33 34 35 36 37 38 | 2x 2x 2x 3x 3x 3x 3x 2x 345x 345x 10721x 345x 2x 2x 2x 2x 7x 7x 337x 337x 134x 7x 5x 2x | const { readFile } = require('../helpers/file')
const { removeEmptyValuesInt } = require('../helpers/array')
const extractData = (fileInput) => {
const data = readFile(fileInput)
const time = removeEmptyValuesInt(data[0].replace('Time:', '').split(' '))
const distance = removeEmptyValuesInt(
data[1].replace('Distance:', '').split(' ')
)
return [time, distance]
}
const getPerformanceMade = (hold, time) => {
let speed = 0
for (let i = 0; i < hold; i++) {
speed++
}
return (time - hold) * speed
}
const resolvePuzzle = (inputFile) => {
const possibilities = []
const [time, distance] = extractData(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 = { extractData, getPerformanceMade, resolvePuzzle }
|