advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

index.js (995B)


      1 const fs = require('fs');
      2 const through = require('through2');
      3 const line_for_line = require('../common/line-for-line');
      4 
      5 let total = 0;
      6 let ribbon = 0;
      7 
      8 fs.createReadStream('input')
      9   .pipe(line_for_line())
     10 
     11   // Business logic
     12   // Convert to dimensions
     13   .pipe(through.obj(function(chunk, enc, cb) {
     14     console.log({ chunk });
     15     this.push(chunk.toString().split('x').map(c => parseInt(c)));
     16     cb();
     17   }))
     18 
     19   // Business logic
     20   // Convert to wrapping paper required for the package
     21   .pipe(through.obj(function(dimensions, enc, cb) {
     22     const w = dimensions[0], h = dimensions[1], l = dimensions[2];
     23     const planes = [w*h,h*l,l*w];
     24     const ribbons = [2*(w+h), 2*(h+l), 2*(l+w)];
     25     total += planes.reduce((r, a) => r + (2*a), 0);
     26     total += Math.min(...planes);
     27     ribbon += Math.min(...ribbons) + (w*h*l);
     28     cb();
     29   }))
     30 
     31   .on('finish', () => {
     32     console.log(`The elfs need ${total} sqft of wrapping paper`);
     33     console.log(`The elfs need ${ribbon} feet of ribbon`);
     34   })