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 (758B)


      1 const fs = require('fs');
      2 const through = require('through2');
      3 const line_for_line = require('../common/line-for-line');
      4 
      5 // Fully expecting other operations later, otherwise would've just kept a single counter
      6 const elfs = [0];
      7 
      8 fs.createReadStream('input')
      9   .pipe(line_for_line())
     10 
     11   // Business logic
     12   .pipe(through(function(line, enc, cb) {
     13     line = line.toString();
     14 
     15     // Handle elf creation
     16     if (!line) {
     17       elfs.push(0);
     18       return cb();
     19     }
     20     const idx = elfs.length - 1;
     21 
     22     // Increase counter
     23     elfs[idx] += parseInt(line);
     24     cb();
     25   }))
     26 
     27   .on('finish', () => {
     28     console.log('Highest', Math.max(...elfs));
     29 
     30     const top3 = elfs.sort((a,b) => b-a).slice(0,3);
     31     console.log('Top3', top3.reduce((a,v) => a+v, 0));
     32   })