-
Notifications
You must be signed in to change notification settings - Fork 17
/
room.js
150 lines (119 loc) · 3.24 KB
/
room.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const jsnx = require('jsnetworkx');
const crypto = require('crypto');
const md5 = str =>
crypto
.createHash('md5')
.update(str)
.digest('hex');
const directionsFromHash = hash => {
const u = hash[0];
const d = hash[1];
const l = hash[2];
const r = hash[3];
return {
U: u === 'b' || u === 'c' || u === 'd' || u === 'e' || u === 'f',
D: d === 'b' || d === 'c' || d === 'd' || d === 'e' || d === 'f',
L: l === 'b' || l === 'c' || l === 'd' || l === 'e' || l === 'f',
R: r === 'b' || r === 'c' || r === 'd' || r === 'e' || r === 'f',
};
};
const directionsFromPasscode = passcode => directionsFromHash(md5(passcode));
class Node {
constructor(seed, depth, x, y, direction = '') {
// Used to create unique nodes
this.seed = seed;
this.depth = depth;
this.x = x;
this.y = y;
this.direction = direction;
}
// @returns {Array<Node>}
generateDeeperNodes(directions) {
const new_nodes = [];
const { U, D, L, R } = directions;
// Up
if (this.y > 1 && U) {
new_nodes.push(
nodeFactory(this.depth + 1, this.x, this.y - 1, 'U')
);
}
// Down
if (this.y < 4 && D) {
new_nodes.push(
nodeFactory(this.depth + 1, this.x, this.y + 1, 'D')
);
}
// Left
if (this.x > 1 && L) {
new_nodes.push(
nodeFactory(this.depth + 1, this.x - 1, this.y, 'L')
);
}
// Right
if (this.x < 4 && R) {
new_nodes.push(
nodeFactory(this.depth + 1, this.x + 1, this.y, 'R')
);
}
return new_nodes;
}
toString() {
return `${this.seed}-${this.x}-${this.y}`;
}
}
const nodeFactory = (() => {
let seed = 0;
return (...args) => new Node(seed++, ...args);
})();
class Room {
constructor(passcode, look_for_shortest = true) {
this.passcode = passcode;
this.graph = new jsnx.DiGraph();
this.origin = nodeFactory(0, 1, 1);
this.targets = [];
this.look_for_shortest = look_for_shortest;
this.graph.addNode(this.origin);
this.generateGraph('', this.origin);
}
generateGraph(path, node) {
if (node.x === 4 && node.y === 4) {
this.targets.push(node);
return;
}
if (this.look_for_shortest && this.targets.length > 0) {
/// If current node is deeper than all solutions, then bail
const might_find_better_path = this.targets.some(
n => node.depth < n.depth
);
if (!might_find_better_path) {
return;
}
}
const directions = directionsFromPasscode(this.passcode + path);
const new_nodes = node.generateDeeperNodes(directions);
for (let new_node of new_nodes) {
this.graph.addEdge(node, new_node);
this.generateGraph(path + new_node.direction, new_node);
}
}
getShortestPath() {
let paths_arr = this.targets.map(end => {
return jsnx.bidirectionalShortestPath(this.graph, this.origin, end);
});
paths_arr.sort((a, b) => a.length - b.length);
let paths = paths_arr.map(path_arr =>
path_arr.reduce((path, node) => path + node.direction, '')
);
return paths[0];
}
getLongestPathLength() {
let paths_arr = this.targets.map(end => {
return jsnx.bidirectionalShortestPath(this.graph, this.origin, end);
});
paths_arr.sort((a, b) => a.length - b.length);
let paths = paths_arr.map(path_arr => path_arr.length);
// Path includes first node, I don't need that
return paths[paths.length - 1] - 1;
}
}
module.exports = { Room, nodeFactory, Node };