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


      1 const fs            = require('fs');
      2 const through       = require('through2');
      3 const char_for_char = require('../common/char-for-char.js');
      4 
      5 const histbuf = [];
      6 let   idx     = 0;
      7 let   L       = 14;
      8 
      9 function uniq(arr) {
     10   return arr.filter((v, i, a) => a.indexOf(v) == i);
     11 }
     12 
     13 fs.createReadStream('input')
     14   .pipe(char_for_char())
     15 
     16   // Business logic
     17   .pipe(through(function(char, enc, cb) {
     18     char = char.toString();
     19 
     20     // Append character
     21     histbuf.push(char);
     22     idx++;
     23 
     24     // Bail if not enough characters
     25     if (histbuf.length < L) {
     26       return cb();
     27     }
     28 
     29     // Limit length
     30     if (histbuf.length > L) {
     31       histbuf.shift();
     32     }
     33 
     34     // Check for all unique characters
     35     if (uniq(histbuf).length == L) {
     36       console.log('START', idx);
     37     }
     38 
     39     // console.log({ histbuf, uniq: uniq(histbuf) });
     40     cb();
     41   }))
     42 
     43   .on('finish', () => {
     44     // done
     45   });