Browse Source

Move render function to common file

ApisNecros 3 months ago
parent
commit
1eda485991
2 changed files with 69 additions and 70 deletions
  1. 1 70
      13/13_1.js
  2. 68 0
      13/common.js

+ 1 - 70
13/13_1.js

@@ -1,5 +1,5 @@
-const util = require("util");
 const fs = require("node:fs");
+const { render } = require("./common");
 const Computer = require("../IntComp/Computer");
 
 // Load and parse the input
@@ -9,77 +9,8 @@ const input = fs.readFileSync("./13/input.dat", "utf8")
 
 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;
-}

+ 68 - 0
13/common.js

@@ -0,0 +1,68 @@
+const gameTiles = [
+    ".", // Empty
+    "█", // Wall
+    "▪", // Block
+    "_", // Paddle
+    "°", // Ball
+];
+
+/**
+ * Render the arcade screen
+ *
+ * @param {number[]} screenState An array of numbers represent the state of the arcade screen
+ * @returns {void}
+ */
+export default 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;
+}