-
Notifications
You must be signed in to change notification settings - Fork 18
/
skynet-revolution1.js
76 lines (68 loc) · 2.15 KB
/
skynet-revolution1.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
function bfs(graph, gateways, agentNodeId) {
let visited = new Set();
let queue = [];
let cameFrom = new Map(); // node id => parent node id on the shortest path
queue.push(agentNodeId);
visited.add(agentNodeId);
while (queue.length !== 0) {
nodeId = queue.shift();
for (let neighborId of graph.get(nodeId)) {
if (!visited.has(neighborId)) {
visited.add(neighborId);
queue.push(neighborId);
cameFrom.set(neighborId, nodeId);
if (gateways.has(neighborId)) {
return [cameFrom, neighborId];
}
}
}
}
return [null, null];
}
function reconstructPath(cameFrom, neighborId) {
let currentId = neighborId;
let stack = [];
while (cameFrom.has(currentId)) {
stack.push(currentId);
currentId = cameFrom.get(currentId);
}
stack.push(currentId);
return stack;
}
// read game input
let graph = new Map(); // node id => links
let gateways = new Set();
let inputs = readline().split(' ');
const nbNodes = parseInt(inputs[0]); // the total number of nodes in the level, including the gateways
const nbLinks = parseInt(inputs[1]); // the number of links
const nbGateways = parseInt(inputs[2]); // the number of exit gateways
for (let i = 0; i < nbLinks; i++) {
let inputs = readline().split(' ');
// n1 and n2 defines a link between these nodes
const n1 = parseInt(inputs[0]);
const n2 = parseInt(inputs[1]);
if (!graph.has(n1)) {
graph.set(n1, new Set());
}
if (!graph.has(n2)) {
graph.set(n2, new Set());
}
graph.get(n1).add(n2);
graph.get(n2).add(n1);
}
for (let i = 0; i < nbGateways; i++) {
gateways.add(parseInt(readline()));
}
// game loop
while (true) {
const agentNodeId = parseInt(readline()); // node id on which the Skynet agent is located
[cameFrom, neighborId] = bfs(graph, gateways, agentNodeId);
if (cameFrom !== null && neighborId !== null) {
path = reconstructPath(cameFrom, neighborId);
secondNodeId = path[path.length - 2];
graph.get(agentNodeId).delete(secondNodeId);
graph.get(secondNodeId).delete(agentNodeId);
}
// the indices of the nodes you wish to sever the link between
console.log(`${agentNodeId} ${secondNodeId}`);
}