Guest User

Untitled

a guest
Sep 23rd, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. let humansAtStart = [];
  2. let zombiesAtStart = [];
  3. let ashAtStart = null;
  4. let bestPath = [];
  5. let bestIndex = 0;
  6. let bestScore = 0;
  7. const zombieSpeed = 400;
  8. const ashSpeed = 1000;
  9. let myTime = null;
  10. let fibonnacciList = [1, 2, 3, 5, 8, 13, 21, 34];
  11.  
  12. const moveToward = (position, goal, speed) => {
  13. let goalVector = minus(goal, position);
  14. if(mag(goalVector) < speed){
  15. return goal;
  16. }
  17. goalVector = normalise(goalVector);
  18. goalVector = mult(goalVector, speed);
  19. let output = floor(plus(position, goalVector));
  20.  
  21. return output;
  22. }
  23.  
  24. const plus = (vector1, vector2) => {
  25. return {x: vector1.x + vector2.x, y: vector1.y + vector2.y};
  26. }
  27.  
  28. const minus = (vector1, vector2) => {
  29. return {x: vector1.x - vector2.x, y: vector1.y - vector2.y};
  30. }
  31.  
  32. const floor = (vector) => {
  33. return {x: Math.floor(vector.x), y: Math.floor(vector.y)};
  34. }
  35.  
  36. const normalise = (vector) => {
  37. return {x:vector.x/mag(vector), y: vector.y/mag(vector)};
  38. }
  39.  
  40. const mag = (vector) => {
  41. return Math.sqrt(Math.pow(vector.x, 2)+Math.pow(vector.y, 2));
  42. }
  43.  
  44. const mult = (vector, num) => {
  45. return {x: vector.x*num, y: vector.y*num};
  46. }
  47.  
  48. const updateHumanThreats = (humans, zombies) => {
  49. return humans.map((human) => {
  50. let min = -1;
  51. let minId = -1;
  52. zombies.forEach((zombie) => {
  53. let distance = dist({x:human.x, y: human.y}, {x:zombie.x, y: zombie.y});
  54. if(min == -1 || min > distance){
  55. min = distance
  56. minId = zombie.id;
  57. }
  58. });
  59. return {...human, closestThreat: minId, distanceToThreat: min};
  60. });
  61.  
  62. }
  63.  
  64. const updateZombieTargets = (zombies, humans, ash) => {
  65. return zombies.map((zombie) => {
  66. let min = -1;
  67. let minHuman = -1;
  68. humans.forEach((human) => {
  69. let distance = dist({x:human.x, y: human.y}, {x:zombie.x, y: zombie.y});
  70. if(min == -1 || min > distance){
  71. min = distance
  72. minHuman = human;
  73. }
  74. });
  75. let distance = dist({x:ashAtStart.x, y: ashAtStart.y}, {x:zombie.x, y: zombie.y});
  76. if(min > distance){
  77. min = distance;
  78. minHuman = ash;
  79. }
  80. return {...zombie, target: {x: minHuman.x, y: minHuman.y}, distance: min};
  81. });
  82. }
  83.  
  84. const gameNotOver = (numOfHumans, numOfZombies) => {
  85. return numOfHumans > 0 && numOfZombies > 0;
  86. }
  87.  
  88. const isTimeLeft = () => {
  89. return Date.now() - myTime < 90;
  90. }
  91.  
  92. const calculateScore = (humansLeft, numberOfZombies) => {
  93. let baseScore = Math.pow(humansLeft, 2)*10;
  94. let score = 0;
  95. for(let i = 0; i < numberOfZombies; i++){
  96. score += baseScore*fibonnacci(i+1);
  97. }
  98. return score;
  99. }
  100.  
  101. const fibonnacci = (n) => {
  102. if(n <= fibonnacciList.length){
  103. return fibonnacciList[n-1];
  104. }
  105. let oldValue = fibonnacciList[fibonnacciList.length-2];
  106. let newValue = fibonnacciList[fibonnacciList.length-1];
  107. for(let i = 0; i < n; i++){
  108. let temp = newValue;
  109. newValue = oldValue + newValue;
  110. oldValue = temp;
  111. fibonnacciList = [...fibonnacciList, newValue];
  112. }
  113. }
  114.  
  115. const dist = (pos1, pos2) => {
  116. return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2));
  117. }
  118.  
  119. const time = (distance, speed) => {
  120. return distance/speed;
  121. }
  122.  
  123. // game loop
  124. while (true) {
  125. humansAtStart = [];
  126. zombiesAtStart = [];
  127. var inputs = readline().split(' ');
  128. const x = parseInt(inputs[0]);
  129. const y = parseInt(inputs[1]);
  130. ashAtStart = {x, y};
  131. const humanCount = parseInt(readline());
  132. for (let i = 0; i < humanCount; i++) {
  133. var inputs = readline().split(' ');
  134. const humanId = parseInt(inputs[0]);
  135. const humanX = parseInt(inputs[1]);
  136. const humanY = parseInt(inputs[2]);
  137. humansAtStart = [...humansAtStart, {x: humanX, y: humanY, id: humanId}];
  138. }
  139. const zombieCount = parseInt(readline());
  140. for (let i = 0; i < zombieCount; i++) {
  141. var inputs = readline().split(' ');
  142. const zombieId = parseInt(inputs[0]);
  143. const zombieX = parseInt(inputs[1]);
  144. const zombieY = parseInt(inputs[2]);
  145. const zombieXNext = parseInt(inputs[3]);
  146. const zombieYNext = parseInt(inputs[4]);
  147. zombiesAtStart = [...zombiesAtStart, {x: zombieX, y: zombieY, nextX: zombieXNext, nextY: zombieYNext, id: zombieId}];
  148. }
  149.  
  150. myTime = Date.now();
  151. zombiesAtStart = updateZombieTargets(zombiesAtStart, humansAtStart, ashAtStart);
  152.  
  153. while(isTimeLeft()){
  154. let humans = [...humansAtStart];
  155. let zombies = [...zombiesAtStart];
  156. let ash = {...ashAtStart};
  157. let score = 0;
  158. let path = [];
  159. while(gameNotOver(humans.length, zombies.length)){
  160. let goal = {x: Math.floor(Math.random()*16000), y: Math.floor(Math.random()*9000)};
  161. ash = moveToward(ash, goal, ashSpeed);
  162. zombies = updateZombieTargets(zombies, humans, ash);
  163. zombies = zombies.map((zombie) => {
  164. let newPos = moveToward({x: zombie.x, y:zombie.y}, zombie.target, zombieSpeed);
  165. return {...zombie, x: newPos.x, y: newPos.y};
  166. })
  167.  
  168. let newZombies = zombies.filter((zombie) => {
  169. return dist({x: zombie.x, y: zombie.y}, ash) > 1000;
  170. })
  171. score += calculateScore(humans.length, zombies.length-newZombies.length);
  172. zombies = [...newZombies];
  173. humans = humans.filter((human) => {
  174. let alive = true;
  175. zombies.forEach((zombie) => {
  176. if(zombie.x == human.x && zombie.y == human.y){
  177. alive = false;
  178. }
  179. })
  180. return alive;
  181. })
  182. path = [...path, goal];
  183. }
  184. if(humans.length > 0){
  185. if(score > bestScore){
  186. bestScore = score;
  187. bestPath = path;
  188. bestIndex = 0;
  189. }else if(score == bestScore && path.length < bestPath.length){
  190. bestScore = score;
  191. bestPath = path;
  192. bestIndex = 0;
  193. }
  194. }
  195.  
  196. }
  197.  
  198. // Write an action using console.log()
  199. // To debug: console.error('Debug messages...');
  200. if(bestIndex == bestPath.length){
  201. bestIndex--;
  202. }
  203. console.error(zombiesAtStart.length);
  204. if(bestScore == 0){
  205. console.log(humansAtStart[0].x + " " + humansAtStart[0].y + " " + bestScore);
  206. }else{
  207. console.log(bestPath[bestIndex].x + " " + bestPath[bestIndex].y + " " + bestScore);
  208. bestIndex++;
  209. }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment