-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.ts
32 lines (29 loc) · 828 Bytes
/
05.ts
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
import SparkMD5 from 'spark-md5';
export type DataType = string;
export const part1 = (data: DataType): string => {
let password = '';
let index = 0;
let hash = '';
while (password.length < 8) {
hash = SparkMD5.hash(`${data}${index}`);
if (hash.startsWith('00000')) {
password += hash.substring(5, 6);
}
index += 1;
}
return password;
};
export const part2 = (data: DataType): string => {
const password = new Array(8).fill('');
let index = 0;
let hash = '';
while (!password.every(p => p !== '')) {
hash = SparkMD5.hash(`${data}${index}`);
const position = Number(hash.substring(5, 6));
if (hash.startsWith('00000') && position < 8 && password[position] === '') {
password[position] = hash.substring(6, 7);
}
index += 1;
}
return password.join('');
};