13_1.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const util = require("util");
  2. const fs = require("node:fs");
  3. const Computer = require("../IntComp/Computer");
  4. // Load and parse the input
  5. const input = fs.readFileSync("./13/input.dat", "utf8")
  6. .split(",")
  7. .map((x) => parseInt(x, 10));
  8. const sampleInput = [104, 1, 104, 2, 104, 3, 104, 6, 104, 5, 104, 4, 99];
  9. const gameTiles = [
  10. ".", // Empty
  11. "█", // Wall
  12. "▪", // Block
  13. "_", // Paddle
  14. "°", // Ball
  15. ];
  16. const arcade = new Computer(input);
  17. arcade.Run();
  18. // console.log(arcade.outputValues);
  19. render(arcade.outputValues);
  20. /**
  21. * Render the arcade screen
  22. *
  23. * @param {number[]} screenState An array of numbers represent the state of the arcade screen
  24. * @returns {void}
  25. */
  26. function render(screenState) {
  27. if (screenState.length % 3 != 0) {
  28. console.warn("screenState length is not divisible by three!");
  29. }
  30. /** @type {number[][]} */
  31. let board = [];
  32. let boardWidth = -1;
  33. let boardHeight = -1;
  34. let blockTileCount = 0;
  35. for (let i = 0; i < screenState.length; i += 3) {
  36. const x = screenState[i];
  37. const y = screenState[i + 1];
  38. const tile = screenState[i + 2];
  39. // Track the board dimensions
  40. if (x > boardWidth) { boardWidth = x; }
  41. if (y > boardHeight) { boardHeight = y; }
  42. // create a row for the y dimension if it doesn't already exist
  43. if (!board[y]) { board[y] = []; }
  44. board[y][x] = tile;
  45. }
  46. board = normalizeBoard(board, boardWidth, boardHeight);
  47. let boardString = "";
  48. for (const row of board) {
  49. for (const tile of row) {
  50. if (tile == 2) {
  51. blockTileCount++;
  52. }
  53. boardString += gameTiles[tile];
  54. }
  55. boardString += "\n";
  56. }
  57. console.clear();
  58. console.log(boardString);
  59. console.log("\n", "Block tiles: ", blockTileCount);
  60. }
  61. function normalizeBoard(board, width, height) {
  62. for (let h = 0; h <= height; h++) {
  63. if (!board[h]) { board[h] = []; }
  64. for (let w = 0; w <= width; w++) {
  65. if (board[h][w] == undefined) { board[h][w] = 0; }
  66. }
  67. }
  68. return board;
  69. }