Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let humansAtStart = [];
- let zombiesAtStart = [];
- let ashAtStart = null;
- let bestPath = [];
- let bestIndex = 0;
- let bestScore = 0;
- const zombieSpeed = 400;
- const ashSpeed = 1000;
- let myTime = null;
- let fibonnacciList = [1, 2, 3, 5, 8, 13, 21, 34];
- const moveToward = (position, goal, speed) => {
- let goalVector = minus(goal, position);
- if(mag(goalVector) < speed){
- return goal;
- }
- goalVector = normalise(goalVector);
- goalVector = mult(goalVector, speed);
- let output = floor(plus(position, goalVector));
- return output;
- }
- const plus = (vector1, vector2) => {
- return {x: vector1.x + vector2.x, y: vector1.y + vector2.y};
- }
- const minus = (vector1, vector2) => {
- return {x: vector1.x - vector2.x, y: vector1.y - vector2.y};
- }
- const floor = (vector) => {
- return {x: Math.floor(vector.x), y: Math.floor(vector.y)};
- }
- const normalise = (vector) => {
- return {x:vector.x/mag(vector), y: vector.y/mag(vector)};
- }
- const mag = (vector) => {
- return Math.sqrt(Math.pow(vector.x, 2)+Math.pow(vector.y, 2));
- }
- const mult = (vector, num) => {
- return {x: vector.x*num, y: vector.y*num};
- }
- const updateHumanThreats = (humans, zombies) => {
- return humans.map((human) => {
- let min = -1;
- let minId = -1;
- zombies.forEach((zombie) => {
- let distance = dist({x:human.x, y: human.y}, {x:zombie.x, y: zombie.y});
- if(min == -1 || min > distance){
- min = distance
- minId = zombie.id;
- }
- });
- return {...human, closestThreat: minId, distanceToThreat: min};
- });
- }
- const updateZombieTargets = (zombies, humans, ash) => {
- return zombies.map((zombie) => {
- let min = -1;
- let minHuman = -1;
- humans.forEach((human) => {
- let distance = dist({x:human.x, y: human.y}, {x:zombie.x, y: zombie.y});
- if(min == -1 || min > distance){
- min = distance
- minHuman = human;
- }
- });
- let distance = dist({x:ashAtStart.x, y: ashAtStart.y}, {x:zombie.x, y: zombie.y});
- if(min > distance){
- min = distance;
- minHuman = ash;
- }
- return {...zombie, target: {x: minHuman.x, y: minHuman.y}, distance: min};
- });
- }
- const gameNotOver = (numOfHumans, numOfZombies) => {
- return numOfHumans > 0 && numOfZombies > 0;
- }
- const isTimeLeft = () => {
- return Date.now() - myTime < 90;
- }
- const calculateScore = (humansLeft, numberOfZombies) => {
- let baseScore = Math.pow(humansLeft, 2)*10;
- let score = 0;
- for(let i = 0; i < numberOfZombies; i++){
- score += baseScore*fibonnacci(i+1);
- }
- return score;
- }
- const fibonnacci = (n) => {
- if(n <= fibonnacciList.length){
- return fibonnacciList[n-1];
- }
- let oldValue = fibonnacciList[fibonnacciList.length-2];
- let newValue = fibonnacciList[fibonnacciList.length-1];
- for(let i = 0; i < n; i++){
- let temp = newValue;
- newValue = oldValue + newValue;
- oldValue = temp;
- fibonnacciList = [...fibonnacciList, newValue];
- }
- }
- const dist = (pos1, pos2) => {
- return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2));
- }
- const time = (distance, speed) => {
- return distance/speed;
- }
- // game loop
- while (true) {
- humansAtStart = [];
- zombiesAtStart = [];
- var inputs = readline().split(' ');
- const x = parseInt(inputs[0]);
- const y = parseInt(inputs[1]);
- ashAtStart = {x, y};
- const humanCount = parseInt(readline());
- for (let i = 0; i < humanCount; i++) {
- var inputs = readline().split(' ');
- const humanId = parseInt(inputs[0]);
- const humanX = parseInt(inputs[1]);
- const humanY = parseInt(inputs[2]);
- humansAtStart = [...humansAtStart, {x: humanX, y: humanY, id: humanId}];
- }
- const zombieCount = parseInt(readline());
- for (let i = 0; i < zombieCount; i++) {
- var inputs = readline().split(' ');
- const zombieId = parseInt(inputs[0]);
- const zombieX = parseInt(inputs[1]);
- const zombieY = parseInt(inputs[2]);
- const zombieXNext = parseInt(inputs[3]);
- const zombieYNext = parseInt(inputs[4]);
- zombiesAtStart = [...zombiesAtStart, {x: zombieX, y: zombieY, nextX: zombieXNext, nextY: zombieYNext, id: zombieId}];
- }
- myTime = Date.now();
- zombiesAtStart = updateZombieTargets(zombiesAtStart, humansAtStart, ashAtStart);
- while(isTimeLeft()){
- let humans = [...humansAtStart];
- let zombies = [...zombiesAtStart];
- let ash = {...ashAtStart};
- let score = 0;
- let path = [];
- while(gameNotOver(humans.length, zombies.length)){
- let goal = {x: Math.floor(Math.random()*16000), y: Math.floor(Math.random()*9000)};
- ash = moveToward(ash, goal, ashSpeed);
- zombies = updateZombieTargets(zombies, humans, ash);
- zombies = zombies.map((zombie) => {
- let newPos = moveToward({x: zombie.x, y:zombie.y}, zombie.target, zombieSpeed);
- return {...zombie, x: newPos.x, y: newPos.y};
- })
- let newZombies = zombies.filter((zombie) => {
- return dist({x: zombie.x, y: zombie.y}, ash) > 1000;
- })
- score += calculateScore(humans.length, zombies.length-newZombies.length);
- zombies = [...newZombies];
- humans = humans.filter((human) => {
- let alive = true;
- zombies.forEach((zombie) => {
- if(zombie.x == human.x && zombie.y == human.y){
- alive = false;
- }
- })
- return alive;
- })
- path = [...path, goal];
- }
- if(humans.length > 0){
- if(score > bestScore){
- bestScore = score;
- bestPath = path;
- bestIndex = 0;
- }else if(score == bestScore && path.length < bestPath.length){
- bestScore = score;
- bestPath = path;
- bestIndex = 0;
- }
- }
- }
- // Write an action using console.log()
- // To debug: console.error('Debug messages...');
- if(bestIndex == bestPath.length){
- bestIndex--;
- }
- console.error(zombiesAtStart.length);
- if(bestScore == 0){
- console.log(humansAtStart[0].x + " " + humansAtStart[0].y + " " + bestScore);
- }else{
- console.log(bestPath[bestIndex].x + " " + bestPath[bestIndex].y + " " + bestScore);
- bestIndex++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment