ソースを参照

Solve day 11 pt 2

Finally, after almost a year I got it.
ApisNecros 4 ヶ月 前
コミット
d72e3a187d
1 ファイル変更24 行追加7 行削除
  1. 24 7
      11/11_2.js

+ 24 - 7
11/11_2.js

@@ -134,17 +134,34 @@ mapData.grid.forEach((color, coords) => {
     if (!imageData[column]) { imageData[column] = []; }
     imageData[column][row] = color;
 });
-// Flatten it into one array, and replace undefined's with 0's
-imageData = imageData.flat().map((color) => (color === undefined ? 0 : color));
+
+// Flatten it into one array, and replace undefined's with null's
+imageData = imageData.map(fillEmptySlotsWithZero)
+    .map((row) => { while (row.length < 43) { row.push(0); } return row; })
+    .flat();
 // Join it into a data stream
 imageData = imageData.join("");
 // Create and render the image
 
+// Height was found from this post: https://old.reddit.com/r/adventofcode/comments/e93d3y/2019_day_11_part_2_animated_python_output/
+// Width was brute forced
 const height = 6;
+const width = 43;
+
+// Render Robby's path as a Space Image
+const identifier = new SpaceImageFormat(width, height, imageData);
+identifier.Render();
+console.log();
 
-for (let width = 40; width < 50; width++) {
-    console.log(`Dimensions: ${width}x${height}`);
-    const identifier = new SpaceImageFormat(width, height, imageData);
-    identifier.Render();
-    console.log();
+/**
+ * Fill empty slots in an array with zeros
+ *
+ * @param {array} arr The array to fill
+ * @returns {array} The input array with no empty spots
+ */
+function fillEmptySlotsWithZero(arr) {
+    return Array.from(arr, (_, i) => {
+        if (!(i in arr)) { return 0; }
+        return arr[i];
+    });
 }