-
Notifications
You must be signed in to change notification settings - Fork 36
/
Skynet-Revolution-ep1.js
69 lines (53 loc) · 1.66 KB
/
Skynet-Revolution-ep1.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
// https://www.codingame.com/training/medium/skynet-revolution-episode-1
var [N, L, E] = readline().split(' ').map(Number);
var graph = new Array(N);
var gates = new Array(E);
for (var i = 0; i < L; i++) {
var [N1, N2] = readline().split(' ').map(Number);
if (graph[N1] === undefined) graph[N1] = [];
if (graph[N2] === undefined) graph[N2] = [];
graph[N1].push(N2);
graph[N2].push(N1);
}
for (var i = 0; i < E; i++) {
gates[i] = +readline();
}
while (true) {
var SI = +readline();
var r1, r2 = SI;
// Check if agent neighbours are close to a gate
var neighbours = graph[SI];
var gate = neighbours.find(n => gates.indexOf(n) !== -1);
if (gate !== undefined) {
r1 = gate;
} else if (neighbours.length === 2) {
// If the agent has two options, cut the one with more neighbours
if (graph[neighbours[0]].length < graph[neighbours[1]].length) {
r1 = neighbours[1];
} else {
r1 = neighbours[0];
}
} else {
// Create a list of gate neighbours having 3 neighbours themselves
var gateNeighbours = [];
for (var g = 0; g < gates.length; g++) {
var gn = graph[gates[g]];
for (var n = 0; n < gn.length; n++) {
if (graph[gn[n]].length === 3) {
gateNeighbours.push(gn[n]);
}
}
}
// Take the first gate neighbour for the moment, could be improved
r1 = gateNeighbours[0];
// Cut on the ring to another neighbour
r2 = graph[r1].find(n => gateNeighbours.indexOf(n) !== -1);
if (r2 === undefined) {
r1 = neighbours[0];
r2 = SI;
}
}
graph[r1] = graph[r1].filter(x => x !== r2);
graph[r2] = graph[r2].filter(x => x !== r1);
print(r1 + ' ' + r2);
}