SHARE
TWEET

day8

a guest Dec 8th, 2019 182 in 136 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. const inputFile = process.argv.includes('TEST') ? './day8-test.txt' : process.argv.includes('BIGBOY') ? './BIGBOY_day8.txt' : './day8.txt';
  4. let debugMode = process.argv.includes('DEBUG');
  5.  
  6. const [width, height] = [25, 6];
  7.  
  8. const data = fs.readFileSync(inputFile).toString().trim();
  9.  
  10. const layerCount = data.length / (width * height);
  11.  
  12. const scanRow = rowN => data.slice(width * rowN, width * rowN + width);
  13.  
  14. function scanLayer(layerN) {
  15.     let res = [];
  16.     for (let n = layerN * height; n < layerN * height + height; ++n) res.push(scanRow(n, width));
  17.     return res;
  18. }
  19.  
  20. function scanLayers() {
  21.     let layers = [];
  22.     for (let i = 0; i < layerCount; ++i) layers.push(scanLayer(i));
  23.     return layers;
  24. }
  25.  
  26. function countDigits(digit, layer) {
  27.     let digitFound = 0;
  28.     for (let row of layer)
  29.         for (let c of row)
  30.             if (c === digit) digitFound++;
  31.     return digitFound;
  32. }
  33.  
  34. const layers = scanLayers();
  35. // part 1
  36. // a really bad way to find the layer with the minimum amount of black
  37. let layerN = 0,
  38.     layerDigs = countDigits('0', layers[0]);
  39. for (let i = 0; i < layers.length; ++i) {
  40.     let digs = countDigits('0', layers[i]);
  41.     if (digs === 0) console.log('aaa: ', layers[i])
  42.     if (digs < layerDigs) {
  43.         layerN = i;
  44.         layerDigs = digs;
  45.     }
  46. }
  47. const answer1 = countDigits('1', layers[layerN]) * countDigits('2', layers[layerN]);
  48. console.log(`part 1: ${answer1}`);
  49. // ight part 2 time
  50. const colors = {
  51.     '0': '█',
  52.     '1': ' ',
  53.     '2': '░'
  54. };
  55. const finalImage = [...Array(height)].map(() => [...Array(width)].map(() => '2'));
  56. for (let layer of layers) {
  57.     for (let x = 0; x < width; ++x) {
  58.         for (let y = 0; y < height; ++y) {
  59.             let pix = layer[y][x];
  60.             if (finalImage[y][x] === '2') finalImage[y][x] = pix;
  61.         }
  62.     }
  63. }
  64. const finalFinalImage = finalImage.map(row => row.map(pixel => colors[pixel])); // thing-final-final-final-final-finalest.pptx
  65. console.log('part 2:');
  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
 
Top