day15 part 1 done
This commit is contained in:
1
2023/day15/example.txt
Normal file
1
2023/day15/example.txt
Normal 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
23
2023/day15/index.js
Normal 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
1
2023/day15/input.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user