|
@@ -1,74 +1,15 @@
|
|
-const util = require("util");
|
|
|
|
|
|
+const fs = require("node:fs");
|
|
|
|
+const { render } = require("./common");
|
|
const Computer = require("../IntComp/Computer");
|
|
const Computer = require("../IntComp/Computer");
|
|
|
|
|
|
-const sampleInput = [104, 1, 104, 2, 104, 3, 104, 6, 104, 5, 104, 4, 99];
|
|
|
|
|
|
+// Load and parse the input
|
|
|
|
+const input = fs.readFileSync("./13/input.dat", "utf8")
|
|
|
|
+ .split(",")
|
|
|
|
+ .map((x) => parseInt(x, 10));
|
|
|
|
|
|
-const gameTiles = [
|
|
|
|
- ".", // Empty
|
|
|
|
- "█", // Wall
|
|
|
|
- "▪", // Block
|
|
|
|
- "_", // Paddle
|
|
|
|
- "°", // Ball
|
|
|
|
-];
|
|
|
|
|
|
+const sampleInput = [104, 1, 104, 2, 104, 3, 104, 6, 104, 5, 104, 4, 99];
|
|
|
|
|
|
-const arcade = new Computer(sampleInput);
|
|
|
|
|
|
+const arcade = new Computer(input);
|
|
arcade.Run();
|
|
arcade.Run();
|
|
|
|
|
|
-// console.log(arcade.outputValues);
|
|
|
|
render(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;
|
|
|
|
-
|
|
|
|
- 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) {
|
|
|
|
- boardString += gameTiles[tile];
|
|
|
|
- }
|
|
|
|
- boardString += "\n";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- console.clear();
|
|
|
|
- console.log(boardString);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-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;
|
|
|
|
-}
|
|
|