Pārlūkot izejas kodu

Begin work on day 13 pt 1

ApisNecros 4 mēneši atpakaļ
vecāks
revīzija
dad684640e
2 mainītis faili ar 164 papildinājumiem un 0 dzēšanām
  1. 74 0
      13/13_1.js
  2. 90 0
      13/sample.icasm

+ 74 - 0
13/13_1.js

@@ -0,0 +1,74 @@
+const util = require("util");
+const Computer = require("../IntComp/Computer");
+
+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(sampleInput);
+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;
+}

+ 90 - 0
13/sample.icasm

@@ -0,0 +1,90 @@
+// Creates the following sample input for y2019d13
+// ####
+// #  #
+// # o#
+// #_ #
+// ####
+
+ld 0d95, 0  // general register
+ld 0d96, 0  // general register
+ld 0d97, 0  // general register
+ld 0d98, 0  // general register
+ld 0d99, 0  // jpt, jpf, eq, lt result register
+
+ld 0d100, 0 // cursor y
+ld 0d101, 0 // cursor x
+
+ld 0d102, 5 // max height
+ld 0d103, 4 // max width
+
+ld 0d110, 0 // empty
+ld 0d111, 1 // wall
+ld 0d112, 2 // block
+ld 0d113, 3 // paddle
+ld 0d114, 4 // ball
+
+ld 0d120, 2 // ball y
+ld 0d121, 2 // ball x
+
+ld 0d130, 3 // paddle y
+ld 0d131, 1 // paddle x
+
+// print ceiling
+PrintCeil:
+out 0d101                   // output x coord
+out 0d100                   // output y coord
+out 0d111                   // output wall tile
+add 0d101, 1, 0d101         // add 1 to x coord
+lt 0d101, 0d103, 0d99       // check if x is at boundary
+jpt 0d99, PrintCeil         // loop function if not
+
+PrintFloor:
+ld 0d100, 0d102             // load the max height into the height
+ld 0d101, 0                 // set the width to 0
+add 0d100, -1, 0d100        // subtract 1 from the height to get it within bounds
+out 0d101                   // output the x coord
+out 0d100                   // output the y coord
+out 0d111                   // output wall tile
+add 0d101, 1, 0d101         // add 1 to the width
+lt 0d101, 0d103, 0d99       // check if x is at the boundary
+jpt 0d99, PrintFloor + 4    // loop function if not 
+
+PrintMiddle:
+ld 0d100, 1                 // set y to 1
+ld 0d101, 0                 // set x to x
+ld 0d95, 0d103              // set reg. a to the max width
+add 0d95, -1, 0d95          // subtract 1 from reg. a
+ld 0d96, 0d102              // set reg. b to max height
+add 0d96, -1, 0d96          // subtract 1 from reg. b
+out 0d101                   // output current x
+out 0d100                   // output current y
+eq 0d101, 0, 0d99           // Check if x is equal to 0
+jpf 0d99, CURR + 3          // jump ahead 2 if not
+out 0d111                   // output a wall tile
+jpt 1, CursorInc            // loop function
+eq 0d101, 0d95, 0d99        // check if x is the boundary
+jpf 0d99, CURR + 3          // jump ahead 3 if not
+out 0d111                   // output an wall tile
+jpt 1, CursorInc            // jump to increment loop
+eq 0d101, 0d121, 0d99       // check if the cursor x matches the ball x
+jpf 0d99, CURR + 5          // skip this section if not
+eq 0d100, 0d120, 0d99       // check if the cursor y matches the ball y
+jpf 0d99, CURR + 3          // skip this section if not
+out 0d114                   // if both checks passed, output the ball tile
+jpt 1, CursorInc            // jump to cursor increment
+eq 0d101, 0d131, 0d99       // check if the cursor x matches the paddle x
+jpf 0d99, CursorInc         // skip this section if not
+eq 0d100, 0d130, 0d99       // check if the cursor y matches the paddle y
+jpf 0d99, CursorInc         // skip this section if not
+out 0d113                   // if both checks passed, output the paddle tile
+CursorInc:
+add 0d101, 1, 0d101         // add 1 to the x
+eq 0d101, 0d95, 0d99        // compare cursor x max width of the board
+jpf 0d99, CURR + ?          // skip this section if not
+ld 0d101, 0                 // reset cursor x to 0
+add 0d100, 1, 0d100         // add 1 to the cursor y
+eq 0d100, 0d96, 0d99        // compare cursor y to max height of the board
+jpt 0d99, Halt              // if they're equal, the whole board has been printed. exit
+jpf 0d99, PrintMiddle + 7   // else, loop the function
+Halt:
+ext                         // exit