-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.js
44 lines (31 loc) · 858 Bytes
/
05.js
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
39
40
41
42
43
44
const crypto = require('crypto')
const md5 = (str) => crypto.createHash('md5').update(str).digest('hex')
const getHash = (str) => {
let hash = ''
for (let i = 0; hash.length < 8; i++) {
const innerHash = md5(str + i)
if (innerHash.slice(0, 5) === '00000') {
hash += innerHash[5]
}
}
return hash
}
const getAdvancedHash = (str) => {
let hash = []
for (let i = 0; hash.filter((x) => x !== undefined).length < 8; i++) {
const innerHash = md5(str + i)
if (innerHash.slice(0, 5) === '00000') {
const index = parseInt(innerHash[5], 16)
if (index < 8 && hash[index] === undefined) {
hash[index] = innerHash[6]
}
}
}
return hash.join('')
}
const part1 = (input) => getHash(input.trim())
const part2 = (input) => getAdvancedHash(input.trim())
module.exports = {
part1,
part2,
}