Skip to content

Instantly share code, notes, and snippets.

@FezVrasta
Created December 3, 2016 15:32
Show Gist options
  • Save FezVrasta/61bd8846180c1aeebcba94e06bdf22ff to your computer and use it in GitHub Desktop.
Save FezVrasta/61bd8846180c1aeebcba94e06bdf22ff to your computer and use it in GitHub Desktop.
CODE vs ZOMBIE
/**
* Save humans, destroy zombies!
**/
const ZOMBIE_RANGE = 400;
const ME_RANGE = 2000;
const ZOMBIE_SPEED = 400;
const ME_SPEED = 1000;
function toString(a) {
return `(${a.id}: ${a.x},${a.y})`;
}
function toCoords(a) {
return `${a.x} ${a.y}`;
}
function isShorter(pos, d1, d2) {
return getScore(pos, d1) < getScore(pos, d2);
}
function getScore(d1, d2) {
d1 = Object.assign({}, d1);
d2 = Object.assign({}, d2);
return Math.sqrt( (d2.x-=d1.x)*d2.x + (d2.y-=d1.y)*d2.y );
}
// game loop
while (true) {
var inputs = readline().split(' ');
var x = parseInt(inputs[0]);
var y = parseInt(inputs[1]);
const pos = {
x,
y,
};
const humans = [];
var humanCount = parseInt(readline());
for (var i = 0; i < humanCount; i++) {
var inputs = readline().split(' ');
var humanId = parseInt(inputs[0]);
var humanX = parseInt(inputs[1]);
var humanY = parseInt(inputs[2]);
humans.push({
id: humanId,
x: humanX,
y: humanY,
});
}
const zombies = [];
var zombieCount = parseInt(readline());
for (var i = 0; i < zombieCount; i++) {
var inputs = readline().split(' ');
var zombieId = parseInt(inputs[0]);
var zombieX = parseInt(inputs[1]);
var zombieY = parseInt(inputs[2]);
var zombieXNext = parseInt(inputs[3]);
var zombieYNext = parseInt(inputs[4]);
zombies.push({
id: zombieId,
x: zombieX,
y: zombieY,
});
}
let priority = {x: Infinity, y: Infinity, d2me: Infinity};
humans.forEach(human => {
if (isShorter(pos, human, priority)) {
const d2me = getScore(pos, human);
priority = {
id: human.id,
x: human.x,
y: human.y,
d2me,
};
}
});
printErr(`priority ${toString(priority)}`);
let toKill = {x: Infinity, y: Infinity};
zombies.forEach(zombie => {
const d2zombie = getScore(priority, zombie);
if (
isShorter(priority, zombie, toKill) &&
(d2zombie - ZOMBIE_RANGE) / ZOMBIE_SPEED > (priority.d2me - ME_RANGE) / ME_SPEED
) {
printErr(d2zombie, priority.d2me);
toKill = {
x: zombie.x,
y: zombie.y,
id: zombie.id,
d2priority: getScore(priority, zombie),
};
}
});
if (!toKill.id) {
toKill = zombies[0];
}
printErr(`toKill ${toString(toKill)}`);
// Write an action using print()
// To debug: printErr('Debug messages...');
print(toCoords(toKill)); // Your destination coordinates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment