13_2.js 950 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const fs = require("node:fs");
  2. const { render } = require("./common");
  3. const Computer = require("../IntComp/Computer");
  4. async function main() {
  5. // Load and parse the input
  6. const input = fs.readFileSync("./13/input.dat", "utf8")
  7. .split(",")
  8. .map((x) => parseInt(x, 10));
  9. // Set the game to autoplay
  10. input[0] = 2;
  11. const arcade = new Computer(input);
  12. arcade.Run();
  13. do {
  14. const [ballX, paddleX] = render(arcade.outputValues, false, true);
  15. let movement = 0;
  16. if (ballX > paddleX) { movement = 1; }
  17. else if (ballX < paddleX) { movement = -1; }
  18. arcade.Input(movement);
  19. // eslint-disable-next-line no-await-in-loop
  20. await sleep(100);
  21. } while (arcade.running);
  22. render(arcade.outputValues);
  23. }
  24. function sleep(ms) {
  25. // eslint-disable-next-line no-promise-executor-return
  26. return new Promise((resolve) => setTimeout(resolve, ms));
  27. }
  28. main();