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


      1 const fs = require('fs');
      2 const through = require('through2');
      3 const char_for_char = require('../common/char-for-char');
      4 
      5 let floor = 0;
      6 let visitedBasement = false;
      7 let ccount = 0;
      8 
      9 fs.createReadStream('input')
     10   .pipe(char_for_char())
     11 
     12   // Business logic
     13   .pipe(through(function(chunk, enc, cb) {
     14     const c = chunk.toString();
     15 
     16     // Track floor
     17     switch(c) {
     18       case '(':
     19         floor++;
     20         break;
     21       case ')':
     22         floor--;
     23         break;
     24     }
     25 
     26     // Track position
     27     ccount++;
     28 
     29     // Check first basement entry
     30     if (floor < 0 && !visitedBasement) {
     31       visitedBasement = true;
     32       console.log(`First entered the basement at instruction ${ccount}`);
     33     }
     34 
     35     cb();
     36   }))
     37 
     38   .on('finish', () => {
     39     console.log(`Santa ends up on floor ${floor}`);
     40   })