day15 part 1 done

This commit is contained in:
2024-12-22 11:27:48 +01:00
parent d81cd73983
commit 021c764d2d
3 changed files with 25 additions and 0 deletions

1
2023/day15/example.txt Normal file
View File

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

23
2023/day15/index.js Normal file
View File

@ -0,0 +1,23 @@
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')

1
2023/day15/input.txt Normal file

File diff suppressed because one or more lines are too long