-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvault2.js
75 lines (56 loc) · 1.69 KB
/
vault2.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const crypto = require('crypto');
const md5 = (value) => crypto.createHash('md5').update(value).digest('hex');
function getMoves(hash) {
return hash
.slice(0, 4)
.split('')
.map((direction) => /[bcdef]/.test(direction));
}
const vectors = {
U: [0, -1],
D: [0, 1],
L: [-1, 0],
R: [1, 0],
};
function move(sequence) {
return sequence.reduce(([px, py], instruction) => {
const [dx, dy] = vectors[instruction];
return [px + dx, py + dy];
}, [0, 0]);
}
function withinField([px, py], [maxX, maxY], direction) {
const [dx, dy] = vectors[direction];
return px + dx >= 0 && px + dx <= maxX && py + dy >= 0 && py + dy <= maxY;
}
function search(seed, to) {
const [, D, , R] = getMoves(md5(seed));
let paths = [];
let nextPaths = [];
// U and L impossible at start
if (D) { paths.push(['D']); }
if (R) { paths.push(['R']); }
let longestPath = 0;
while (paths.length) {
nextPaths = [];
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
const [px, py] = move(path);
if (px === to[0] && py === to[1]) {
if (path.length > longestPath) {
longestPath = path.length;
}
continue;
}
const [U, D, L, R] = getMoves(md5(`${seed}${path.join('')}`));
U && withinField([px, py], to, 'U') && nextPaths.push([...path, 'U']);
D && withinField([px, py], to, 'D') && nextPaths.push([...path, 'D']);
L && withinField([px, py], to, 'L') && nextPaths.push([...path, 'L']);
R && withinField([px, py], to, 'R') && nextPaths.push([...path, 'R']);
}
paths = nextPaths;
}
return longestPath;
}
module.exports = (seed, target = [3, 3]) => {
return search(seed, target);
};