-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
40 lines (31 loc) · 906 Bytes
/
part2.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
33
34
35
36
37
38
39
40
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n').map(Number);
const hashRepeats = 2000;
const hash = (n: number) => {
n = (n ^ (n * 64)) & 16777215;
n = (n ^ (n / 32)) & 16777215;
return (n ^ (n * 2048)) & 16777215;
}
let secrets: number[] = [...input];
let diffs = input.map(() => []);
let cache = new Map();
let result = 0;
for (let i = 0; i < hashRepeats; i++) {
secrets = secrets.map((secret, j) => {
let newSecret = hash(secret);
diffs[j].push(Number((newSecret % 10) - (secret % 10)));
if (diffs[j].length === 4) {
let key = diffs[j].join(",");
let value = cache.get(key) || { sum: 0, set: new Set() };
if (!value.set.has(j)) {
value.set.add(j);
value.sum += Number(newSecret % 10);
result = Math.max(result, value.sum);
}
cache.set(key, value);
diffs[j].shift();
}
return newSecret;
});
}
console.log(result);