SHARE
TWEET

day8.js

a guest Dec 8th, 2019 90 in 135 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const assert = require('assert');
  3.  
  4. const inputFile = process.argv.includes('TEST') ? './day8-test.txt' : process.argv.includes('BIGBOY') ? './BIGBOY_day8.txt' : './day8.txt';
  5. let debugMode = process.argv.includes('DEBUG');
  6.  
  7. const [width, height] = [25, 6];
  8.  
  9. const data = fs.readFileSync(inputFile).toString().trim();
  10.  
  11. const layerCount = data.length / (width * height);
  12.  
  13. function scanRow(rowN) {
  14.     return data.slice(width * rowN, width * rowN + width);
  15. }
  16.  
  17. function scanLayer(layerN) {
  18.     let res = [];
  19.     for (let n = layerN * height; n < layerN * height + height; ++n) res.push(scanRow(n, width));
  20.     return res;
  21. }
  22.  
  23. function scanLayers() {
  24.     let layers = [];
  25.     for (let i = 0; i < layerCount; ++i) layers.push(scanLayer(i));
  26.     return layers;
  27. }
  28.  
  29. function countDigits(digit, layer) {
  30.     let digitFound = 0;
  31.     for (let row of layer)
  32.         for (let c of row)
  33.             if (c === digit) digitFound++;
  34.     return digitFound;
  35. }
  36.  
  37. const layers = scanLayers();
  38. assert(layerCount === layers.length);
  39. let layerN = 0, layerDigs = countDigits('0', layers[0]);
  40. for (let i = 0; i < layers.length; ++i) {
  41.     let digs = countDigits('0', layers[i]);
  42.     if (digs === 0) console.log('aaa: ', layers[i])
  43.     if (digs < layerDigs) {
  44.         layerN = i;
  45.         layerDigs = digs;
  46.     }
  47. }
  48. const answer1 = countDigits('1', layers[layerN]) * countDigits('2', layers[layerN]);
  49. console.log(answer1);
  50. const colors = {'0': '*', '1': '-', '2': ' '};
  51.  
  52. const finalImage = [...Array(height)].map(() => [...Array(width)].map(() => '2'));
  53.  
  54. for (let layer of layers) {
  55.     for (let x = 0; x < width; ++x) {
  56.         for (let y = 0; y < height; ++y) {
  57.             let pix = layer[y][x];
  58.             if (finalImage[y][x] === 2) finalImage[y][x] = pix;
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. const finalFinalImage = finalImage.map(row => row.map(pixel => colors[pixel]));
  65. assert(finalFinalImage.length == height);
  66. for (let row of finalFinalImage) {
  67.     console.log(row.join(''));
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top