12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- const util = require("util");
- const fs = require("node:fs");
- const Computer = require("../IntComp/Computer");
- // Load and parse the input
- const input = fs.readFileSync("./13/input.dat", "utf8")
- .split(",")
- .map((x) => parseInt(x, 10));
- const sampleInput = [104, 1, 104, 2, 104, 3, 104, 6, 104, 5, 104, 4, 99];
- const gameTiles = [
- ".", // Empty
- "█", // Wall
- "▪", // Block
- "_", // Paddle
- "°", // Ball
- ];
- const arcade = new Computer(input);
- arcade.Run();
- // console.log(arcade.outputValues);
- render(arcade.outputValues);
- /**
- * Render the arcade screen
- *
- * @param {number[]} screenState An array of numbers represent the state of the arcade screen
- * @returns {void}
- */
- function render(screenState) {
- if (screenState.length % 3 != 0) {
- console.warn("screenState length is not divisible by three!");
- }
- /** @type {number[][]} */
- let board = [];
- let boardWidth = -1;
- let boardHeight = -1;
- let blockTileCount = 0;
- for (let i = 0; i < screenState.length; i += 3) {
- const x = screenState[i];
- const y = screenState[i + 1];
- const tile = screenState[i + 2];
- // Track the board dimensions
- if (x > boardWidth) { boardWidth = x; }
- if (y > boardHeight) { boardHeight = y; }
- // create a row for the y dimension if it doesn't already exist
- if (!board[y]) { board[y] = []; }
- board[y][x] = tile;
- }
- board = normalizeBoard(board, boardWidth, boardHeight);
- let boardString = "";
- for (const row of board) {
- for (const tile of row) {
- if (tile == 2) {
- blockTileCount++;
- }
- boardString += gameTiles[tile];
- }
- boardString += "\n";
- }
- console.clear();
- console.log(boardString);
- console.log("\n", "Block tiles: ", blockTileCount);
- }
- function normalizeBoard(board, width, height) {
- for (let h = 0; h <= height; h++) {
- if (!board[h]) { board[h] = []; }
- for (let w = 0; w <= width; w++) {
- if (board[h][w] == undefined) { board[h][w] = 0; }
- }
- }
- return board;
- }
|