Skip to content

Instantly share code, notes, and snippets.

@jwcarroll
Created June 5, 2015 04:17
Show Gist options
  • Save jwcarroll/de4ef29f2dcdd5323617 to your computer and use it in GitHub Desktop.
Save jwcarroll/de4ef29f2dcdd5323617 to your computer and use it in GitHub Desktop.
Coding Game: Skynet the virus (solution)
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
var mainNetwork = initializeNetwork();
// game loop
while (true) {
var agentIndex = parseInt(readline());
var skynetAgent = new SkynetAgent(agentIndex,mainNetwork);
var linkToSever = skynetAgent.findImmediateLinkToGateway() ||
mainNetwork.getRandomGatewayLink();
linkToSever.sever();
}
function initializeNetwork(){
var network = new Network();
var inputs = readline().split(' ');
var N = parseInt(inputs[0]); // the total number of nodes in the level, including the gateways
var L = parseInt(inputs[1]); // the number of links
var E = parseInt(inputs[2]); // the number of exit gateways
network.addNodes(N);
for (var i = 0; i < L; i++) {
var inputs = readline().split(' ');
var N1 = parseInt(inputs[0]); // N1 and N2 defines a link between these nodes
var N2 = parseInt(inputs[1]);
var node1 = network.getNode(N1);
var node2 = network.getNode(N2);
node1.linkTo(node2);
node2.linkTo(node1);
}
for (var i = 0; i < E; i++) {
var EI = parseInt(readline()); // the index of a gateway node
network.getNode(EI).markGateway();
}
return network;
}
function NodeLink(n1, n2){
node1 = n1;
node2 = n2;
return {
sever:function(){
n1.removeLink(n2);
n2.removeLink(n1);
print(n1.name() + ' ' + n2.name());
}
}
}
function SkynetAgent(index, network){
var _index = index,
_network = network;
return {
findImmediateLinkToGateway: function(){
return _network.getNode(_index).findLinkedGateway();
}
};
}
function Network(){
var nodes = [];
return {
addNodes: function(numNodes){
for(var i = 0; i < numNodes; i++){
this.addNode(i);
}
},
addNode: function(index){
var newNode = new Node(index);
nodes[index] = newNode;
return newNode;
},
getNode: function(index){
return nodes[index];
},
getRandomGatewayLink: function(){
var firstGatewayWithLinks = nodes.find(isGatewayWithLinks);
return firstGatewayWithLinks.getFirstLink();
}
};
function isGatewayWithLinks(node){
return node.isGateway() && node.hasLinks();
}
}
function Node(name){
var _links = [],
_name = name,
_isGateway = false;
return {
name:function(){
return _name;
},
linkTo:function(node){
var linkedNode = _links.find(function(curNode){
return node === curNode;
});
if(!linkedNode){
_links.push(node);
}
},
removeLink:function(node){
remove(_links, node);
},
getFirstLink:function(){
return new NodeLink(this, _links[0]);
},
hasLinks:function(){
return _links.length > 0;
},
markGateway:function(){
_isGateway = true;
},
isGateway:function(){
return _isGateway;
},
findLinkedGateway:function(){
var linkedGatewayNode = _links.find(isGatewayNode);
if(linkedGatewayNode){
return new NodeLink(this, linkedGatewayNode);
}
}
};
function isGatewayNode(node){
return node.isGateway();
}
}
function remove(array, item){
var index = array.indexOf(item);
if(index > -1){
array.splice(index, 1);
}
}
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment