23 lines
512 B
JavaScript
23 lines
512 B
JavaScript
import fs from 'node:fs'
|
|
|
|
function init(filename) {
|
|
const data = fs.readFileSync(filename, { encoding: 'utf-8' }, data => data)
|
|
|
|
const words = [...data.split(',')]
|
|
const result = words.map(word => hash(word))
|
|
.reduce((prev, cur) => prev + cur, 0)
|
|
console.log(result)
|
|
}
|
|
|
|
function hash(word) {
|
|
|
|
let result = 0
|
|
for (let i = 0; i < String(word).length; i++) {
|
|
result += word.charCodeAt(i)
|
|
result *= 17
|
|
result %= 256
|
|
}
|
|
return result
|
|
}
|
|
|
|
init('input.txt') |