Guest User

Untitled

a guest
Dec 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. const [WALL, EMPTY, ELF, GOB] = [0, 1, 'elf', 'gob'];
  4. const maxDist = 200;
  5.  
  6. const ents = [];
  7. const coord = (x, y) => ({ x, y });
  8. const delta = (pos, x, y) => ({ x: pos.x + x, y: pos.y + y });
  9. const cell = pos => board[pos.y][pos.x];
  10. const setCell = (pos, v) => board[pos.y][pos.x] = v;
  11. const addEnt = (x, y, type) => {
  12. const ent = { x, y, hp: 200, type };
  13. ents.push(ent);
  14. return ent;
  15. }
  16.  
  17. console._log = console.constructor.prototype.log.bind(console);
  18. console.log = () => {};
  19. const board =
  20. fs.readFileSync('input', 'utf-8')
  21. .split('\n')
  22. .map((l, y) =>
  23. l.split('')
  24. .map((c, x) => {
  25. switch (c) {
  26. case '#': return WALL;
  27. case '.': return EMPTY;
  28. case 'E': return addEnt(x, y, ELF);
  29. case 'G': return addEnt(x, y, GOB);
  30. }
  31. console.log('wut', c, x, y)
  32. }));
  33.  
  34. const log = () => {
  35. let str = board.map(l => l.map(c =>
  36. c === WALL ? '#'
  37. : c === EMPTY ? '.'
  38. : c.type === ELF ? 'E'
  39. : c.type === GOB ? 'G'
  40. : '?'
  41. ).join('')).join('\n')
  42. console._log(str);
  43. console._log(ents.map(e => [e.type, e.hp]).join('|'))
  44. }
  45.  
  46.  
  47. const getNeighbours = pos => [
  48. delta(pos, 0, -1),
  49. delta(pos, -1, 0),
  50. delta(pos, 1, 0),
  51. delta(pos, 0, 1)
  52. ];
  53.  
  54. const checkTile = (ent, pos) => {
  55.  
  56. const checked = [ent, pos];
  57. let toCheck = getNeighbours(pos);
  58. let nextCheck = [];
  59.  
  60. let dist = 0;
  61. distCheck:
  62. while(++dist < maxDist) {
  63. for(const pos of toCheck) {
  64. const v = cell(pos);
  65.  
  66. if (v === EMPTY) {
  67. const neighbours = getNeighbours(pos)
  68. .filter(p => {
  69. if (checked.some(q => q.x === p.x && q.y === p.y) || nextCheck.some(q => q.x === p.x && q.y === p.y)) {
  70. return false;
  71. }
  72.  
  73. const c = cell(p);
  74.  
  75. return c === EMPTY || c.dead || (c.type && c.type !== ent.type);
  76. });
  77.  
  78. nextCheck.push(...neighbours)
  79. }
  80.  
  81. if (!v.dead && v.type && v.type !== ent.type) {
  82. break distCheck;
  83. }
  84.  
  85. checked.push(pos);
  86. };
  87. toCheck = nextCheck;
  88. nextCheck = [];
  89. }
  90.  
  91. return dist;
  92. }
  93. const getNextTile = ent => {
  94. const tiles = getNeighbours(ent)
  95. .filter(p => cell(p) === EMPTY)
  96. .map(p => ({ x: p.x, y: p.y, v: checkTile(ent, p) }))
  97. .filter(c => c.v < maxDist);
  98.  
  99. return tiles.sort((a, b) => a.v - b.v || a.y - b.y || a.x - b.x)[0];
  100. }
  101.  
  102. const getNextEnemy = ent => {
  103. return getNeighbours(ent)
  104. .map(p => cell(p))
  105. .filter(c => !c.dead && c.type && c.type !== ent.type)
  106. .sort((a, b) => a.hp - b.hp || a.y - b.y || a.x - b.x)
  107. [0];
  108. }
  109.  
  110. let rounds = 0;
  111. loop: while(++rounds < 10000) {
  112. ents.sort((a, b) => a.y - b.y || a.x - b.x);
  113.  
  114. log()
  115.  
  116. for(let i = 0; i < ents.length; ++i) {
  117. const ent = ents[i];
  118. console.log('trying', i, ent);
  119. console.group();
  120.  
  121. if (ent.dead) {
  122. console.groupEnd();
  123. continue;
  124. }
  125.  
  126. const enemy = getNextEnemy(ent);
  127.  
  128. if (enemy) {
  129. console.log({ enemy })
  130. enemy.hp -= 3;
  131.  
  132. if (enemy.hp <= 0) {
  133. enemy.dead = true;
  134. }
  135. } else {
  136. const nextTile = getNextTile(ent);
  137. console.log({ nextTile })
  138.  
  139. if (nextTile) {
  140. setCell(ent, EMPTY);
  141.  
  142. ent.x = nextTile.x;
  143. ent.y = nextTile.y;
  144.  
  145. setCell(ent, ent);
  146. }
  147.  
  148. const enemy = getNextEnemy(ent);
  149.  
  150. if (enemy) {
  151. enemy.hp -= 3;
  152.  
  153. if (enemy.hp <= 0) {
  154. enemy.dead = true;
  155. }
  156. }
  157. }
  158. console.groupEnd();
  159. }
  160.  
  161. let type = ents[0].type;
  162. let allTypesSame = true;
  163. for(let i = 0; i < ents.length; ++i) {
  164. const ent = ents[i];
  165.  
  166. if (ent.dead) {
  167. setCell(ent, EMPTY);
  168. ents.splice(i, 1);
  169. --i;
  170. } else {
  171. if (type !== ent.type) {
  172. allTypesSame = false;
  173. }
  174. }
  175. }
  176.  
  177. if (allTypesSame) {
  178. console._log('ended in ' + rounds);
  179. const sum = ents.reduce((a, e) => a + e.hp, 0);
  180. console._log(sum, sum * (rounds-1));
  181. //console._log(ents);
  182.  
  183. break;
  184. }
  185. }
Add Comment
Please, Sign In to add comment