Skip to content

Instantly share code, notes, and snippets.

@paulsancer
Last active October 3, 2022 10:46
Show Gist options
  • Save paulsancer/b52c15338e0f3be5c2b963791435dd86 to your computer and use it in GitHub Desktop.
Save paulsancer/b52c15338e0f3be5c2b963791435dd86 to your computer and use it in GitHub Desktop.
Solution to Code VS Zombies challenge at https://www.codingame.com/ide/puzzle/code-vs-zombies.
/**
* Save humans, destroy zombies!
* https://www.codingame.com/ide/puzzle/code-vs-zombies
**/
const spartanSpeed = 1000;
const zombieSpeed = 400;
const killingDistance = 2000;
const speedDifferenceMultiplier = spartanSpeed / zombieSpeed;
const getDistance = (pointA, pointB) =>
Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
// game loop
while (true) {
let humans = [];
const zombies = [];
const spartanCoordinates = readline().split(' ');
const masterChief = {
x: spartanCoordinates[0],
y: spartanCoordinates[1],
nextX: 0,
nextY: 0
};
console.error('Master Chief 😎: ', masterChief);
console.error(
'************************** New Turn Starts ⚔️ ***************************'
);
const humanCount = parseInt(readline());
for (let i = 0; i < humanCount; i++) {
const inputs = readline().split(' ');
const human = {
id: parseInt(inputs[0]),
x: parseInt(inputs[1]),
y: parseInt(inputs[2]),
distance: 0,
alive: true
};
human.distance = getDistance(masterChief, human);
humans.push(human);
}
humans = humans.sort((a, b) => a.distance - b.distance); // necessary?
let ryan = null; // ryan or nemo?
const zombieCount = parseInt(readline());
for (let i = 0; i < zombieCount; i++) {
const inputs = readline().split(' ');
// if we already have a ryan/nemo, skip the rest of the zombies
if (ryan) continue;
const zombie = {
id: parseInt(inputs[0]),
x: parseInt(inputs[1]),
y: parseInt(inputs[2]),
nextX: parseInt(inputs[3]),
nextY: parseInt(inputs[4])
};
console.error(`Zombie #${zombie.id} vs humans: `, zombie);
// can Master Chief reach any of the humans before this zombie? find nemo!
let ryans = humans.filter(h => {
console.error('---');
console.error('Human: ', h);
const hDistance = getDistance(zombie, h);
console.error('Distance (zombie to human): ', hDistance);
const isSpartanCloser = h.distance - killingDistance < hDistance;
console.error('Is Master Chief Closer?: ', isSpartanCloser);
return isSpartanCloser;
});
console.error('Ryans: ', ryans);
// any ryans so far?
if (ryans.length > 0) {
// found a ryan!
ryans = ryans.sort((a, b) => a.distance - b.distance);
console.error(`Ryans: `, ryans);
ryan = ryans[0];
console.error(
'- Cortana: Master Chief, we found Nemo! 🚨 RESCUE RYAN! 🚨'
);
// (this metaphor is out of control... 😅)
}
zombies.push(zombie); // do I need to store zombies?
console.error('-----------------------------------------');
}
const next = ryan || humans[0];
masterChief.nextX = next.x;
masterChief.nextY = next.y;
// Write an action using console.log()
// To debug: console.error('Debug messages...');
console.log(`${masterChief.nextX} ${masterChief.nextY}`); // Your destination coordinates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment