10 Коміти dad684640e ... 2078d2de47

Автор SHA1 Опис Дата
  ApisNecros 2078d2de47 Add day 13 pt 2 solution 3 місяців тому
  ApisNecros f8b2f8df0b Remove unused parameters from render function 3 місяців тому
  ApisNecros 13e43246eb Remove optional parameter from render call 3 місяців тому
  ApisNecros 24d2ae6754 Refactor render function 3 місяців тому
  ApisNecros 49290d5f93 Restore input to original state 3 місяців тому
  ApisNecros a34f2c8173 Add toggle for block count 3 місяців тому
  ApisNecros 845cf8fb24 Start on day 13 pt 2 solution 3 місяців тому
  ApisNecros b4a938e231 Fix module exports 3 місяців тому
  ApisNecros 1eda485991 Move render function to common file 3 місяців тому
  ApisNecros a72af90905 Add solution for day 13 pt 1 3 місяців тому
4 змінених файлів з 136 додано та 67 видалено
  1. 8 67
      13/13_1.js
  2. 38 0
      13/13_2.js
  3. 90 0
      13/common.js
  4. 0 0
      13/input.dat

+ 8 - 67
13/13_1.js

@@ -1,74 +1,15 @@
-const util = require("util");
+const fs = require("node:fs");
+const { render } = require("./common");
 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();
 
-// 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;
-
-    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;
-}

+ 38 - 0
13/13_2.js

@@ -0,0 +1,38 @@
+const fs = require("node:fs");
+const { render } = require("./common");
+const Computer = require("../IntComp/Computer");
+
+async function main() {
+    // Load and parse the input
+    const input = fs.readFileSync("./13/input.dat", "utf8")
+        .split(",")
+        .map((x) => parseInt(x, 10));
+
+    // Set the game to autoplay
+    input[0] = 2;
+
+    const arcade = new Computer(input);
+    arcade.Run();
+
+    do {
+        const [ballX, paddleX] = render(arcade.outputValues, false, true);
+        let movement = 0;
+
+        if (ballX > paddleX) { movement = 1; }
+        else if (ballX < paddleX) { movement = -1; }
+
+        arcade.Input(movement);
+
+        // eslint-disable-next-line no-await-in-loop
+        await sleep(100);
+    } while (arcade.running);
+
+    render(arcade.outputValues);
+}
+
+function sleep(ms) {
+    // eslint-disable-next-line no-promise-executor-return
+    return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+main();

+ 90 - 0
13/common.js

@@ -0,0 +1,90 @@
+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
+ * @param {boolean} autoplay If true, the game will autoplay itself
+ * @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;
+    let gameScore = 0;
+    let ballX = 0;
+    let paddleX = 0;
+
+    for (let i = 0; i < screenState.length; i += 3) {
+        const x = screenState[i];
+        const y = screenState[i + 1];
+        const tile = screenState[i + 2];
+
+        if (x == -1) { gameScore = tile; }
+
+        // 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] = []; }
+
+        // If the current tile is the ball, store the x coord for autoplay
+        if (tile == 3) { paddleX = x; }
+        else if (tile == 4) { ballX = x; }
+
+        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";
+    }
+
+    return [boardString, gameScore, blockTileCount, ballX, paddleX];
+}
+
+function renderToConsole(screenState) {
+    const [boardState, gameScore, blockTileCount, ballX, paddleX] = _render(screenState);
+    console.clear();
+    console.log("Score: ", gameScore);
+    console.log(boardState);
+    console.log("Block Tile Count: ", blockTileCount);
+
+    return [ballX, paddleX];
+}
+
+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;
+}
+
+module.exports = {
+    render: renderToConsole,
+};

Різницю між файлами не показано, бо вона завелика
+ 0 - 0
13/input.dat


Деякі файли не було показано, через те що забагато файлів було змінено