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


      1 const fs = require('fs');
      2 const through = require('through2');
      3 const line_for_line = require('../common/line-for-line');
      4 
      5 let remainder = 0;
      6 
      7 fs.createReadStream('input')
      8   .pipe(line_for_line())
      9 
     10   // Convert to string
     11   .pipe(through(function(line, enc, cb) {
     12     this.push(line.toString());
     13     cb();
     14   }))
     15 
     16   // // Bad if invalid combinations are found
     17   // .pipe(through(function(line, enc, cb) {
     18   //   line = line.toString();
     19   //   const badStrings = [
     20   //     'ab',
     21   //     'cd',
     22   //     'pq',
     23   //     'xy',
     24   //   ];
     25   //   for(const badString of badStrings) {
     26   //     if (~line.indexOf(badString)) {
     27   //       return cb();
     28   //     }
     29   //   }
     30   //   this.push(line);
     31   //   cb()
     32   // }))
     33 
     34   // // Containing less than 3 vowels = not nice
     35   // .pipe(through(function(line, enc, cb) {
     36   //   line = line.toString();
     37   //   const vowels = (line.match(/[aeiou]/g) || []).length;
     38   //   if (vowels < 3) {
     39   //     return cb();
     40   //   }
     41   //   this.push(line);
     42   //   cb();
     43   // }))
     44 
     45   // // Must have 2 of the same characters in a row
     46   // .pipe(through(function(line, enc, cb) {
     47   //   line = line.toString();
     48   //   const matches = (line.match(/([a-z])\1+/g) || []).length;
     49   //   if (!matches) {
     50   //     return cb();
     51   //   }
     52   //   this.push(line);
     53   //   cb();
     54   // }))
     55 
     56   // Must match ?.?, where ? is the same
     57   .pipe(through(function(line, enc, cb) {
     58     line = line.toString();
     59     const matches = (line.match(/([a-z]).\1/g) || []).length;
     60     if (!matches) {
     61       return cb();
     62     }
     63     this.push(line);
     64     cb();
     65   }))
     66 
     67   // Must have non-overlapping character pair
     68   .pipe(through(function(line, enc, cb) {
     69     line = line.toString();
     70     let matched = false;
     71     for(let i=0; i<(line.length-1); i++) {
     72       const set = line.substr(i, 2);
     73       const idx = line.indexOf(set);
     74       if ((idx >= 0) && (idx < (i-1))) {
     75         matched = true;
     76         break;
     77       }
     78     }
     79     if (!matched) {
     80       return cb();
     81     }
     82     this.push(line);
     83     cb();
     84   }))
     85 
     86   .pipe(through(function(line, enc, cb) {
     87     remainder++;
     88     this.push(line);
     89     cb();
     90   }))
     91 
     92   .on('finish', () => {
     93     console.log('finished', remainder);
     94   })