All files / day15 puzzle1.js

100% Statements 17/17
100% Branches 0/0
100% Functions 3/3
100% Lines 17/17

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 262x   2x 6029x 6029x 6029x 25473x   6029x     2x 2x 2x 2x 4011x   2x     2x 1x 1x   2x  
const { readFile } = require('../helpers/file')
 
const runHashAlgorithm = (str) => {
  const charArray = [...str]
  let value = 0
  for (const char of charArray) {
    value = ((value + char.charCodeAt(0)) * 17) % 256
  }
  return value
}
 
const getSumOfHashValues = (str) => {
  const elements = str.split(',')
  let sum = 0
  for (const element of elements) {
    sum += runHashAlgorithm(element)
  }
  return sum
}
 
const resolvePuzzle = (fileInput) => {
  const data = readFile(fileInput)[0]
  return getSumOfHashValues(data)
}
module.exports = { resolvePuzzle, runHashAlgorithm, getSumOfHashValues }