소스 검색

Finish day 8 part 2

With the addition of the render function to the Space Image Format, day
8 part 2 is complete.
ApisNecros 1 년 전
부모
커밋
e03749089d
1개의 변경된 파일33개의 추가작업 그리고 32개의 파일을 삭제
  1. 33 32
      8/8_2.js

+ 33 - 32
8/8_2.js

@@ -1,5 +1,7 @@
 /* eslint-disable max-classes-per-file */
 
+const { DeepClone } = require("../IntComp/common");
+
 class SpaceImageFormatLayer {
     /**
      * A layer of a Space Image Format image
@@ -55,16 +57,42 @@ class SpaceImageFormat {
          * @type {SpaceImageFormatLayer[]}
          */
         this.layers = this._parse(rawData);
+
+        this.PIXEL_DEPTH = ["░", "▓"];
     }
 
     /**
-     * Render an image
+     * Render an image to the console
+     *
+     * Given the simplicity of the image format's color space, we can easily
+     * accomplish this using the "light shade" and "dark shade" ASCII characters.
      * @returns {void}
      */
     Render() {
-        for (let row = 0; row < this.height; row++) {
-            
+        // Will be filled with a string containing the final image
+        let renderedImage = "";
+        // Get the first layer's rows
+        let renderedLayerRows = DeepClone(this.layers[0].rows);
+        // Iterate over subsequent layers
+        for (let i = 1; i < this.layers.length; i++) {
+            const layer = this.layers[i];
+
+            for (let row = 0; row < renderedLayerRows.length; row++) {
+                for (let column = 0; column < renderedLayerRows[row].length; column++) {
+                    if (renderedLayerRows[row][column] == 2 && layer.rows[row][column] !== 2) { renderedLayerRows[row][column] = layer.rows[row][column]; }
+                }
+            }
+
+            // Check if all transparent pixels have been filled in
+            if (renderedLayerRows.flat(Infinity).indexOf(2) == -1) { break; }
         }
+
+        // Convert each row from an array of 0's and 1's to a string
+        renderedLayerRows = renderedLayerRows.map((row) => row.map((pixelValue) => this.PIXEL_DEPTH[pixelValue]).join(""));
+        // Join all rows with new lines to complete the image
+        renderedImage = renderedLayerRows.join("\n");
+
+        console.log(renderedImage);
     }
 
     /**
@@ -98,36 +126,9 @@ const input = {
 const demo = {
     width: 3,
     height: 2,
-    data: "123456789012",
+    data: "122012011120",
 };
 
 const image = new SpaceImageFormat(input.width, input.height, input.data);
 
-/* Find layer with fewest zeros */
-let layerWithLeastZeros = 0;
-let zerosInLayer = 999;
-
-for (let layer = 0; layer < image.layers.length; layer++) {
-    const zeroCount = image.layers[layer]._rawData.replace(/[^0]/g, "").length;
-
-    if (zeroCount < zerosInLayer) {
-        layerWithLeastZeros = layer;
-        zerosInLayer = zeroCount;
-    }
-}
-
-console.log(`Layer with the least amount of 0's: ${layerWithLeastZeros}`);
-
-/**
- * Get the image checksum
- *
- * This is the number of `1`'s multiplied by the number of `2`'s on
- * the layer with least amount of `0`'s
- */
-
-const layer = image.layers[layerWithLeastZeros];
-
-const countOfOnes = layer._rawData.replace(/[^1]/g, "").length;
-const countOfTwos = layer._rawData.replace(/[^2]/g, "").length;
-
-console.log(`Image checksum: ${countOfOnes * countOfTwos}`);
+image.Render();