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


      1 const fs            = require('fs');
      2 const through       = require('through2');
      3 const line_for_line = require('../common/line-for-line');
      4 
      5 let priority      = 0;
      6 let priorityGroup = 0;
      7 
      8 let startA = 'a'.charCodeAt(0);
      9 let stopA  = 'z'.charCodeAt(0);
     10 let startB = 'A'.charCodeAt(0);
     11 let stopB  = 'Z'.charCodeAt(0);
     12 
     13 fs.createReadStream('input')
     14   .pipe(line_for_line())
     15 
     16   // Business logic
     17   .pipe(through(function(line, enc, cb) {
     18     line = line.toString();
     19 
     20     const compartmentA = line.substr(0, line.length / 2);
     21     const compartmentB = line.substr(line.length / 2);
     22 
     23     for(let i=0; i<compartmentA.length; i++) {
     24       const c = compartmentA.substr(i, 1);
     25       const C = c.charCodeAt(0);
     26       if (!~compartmentB.indexOf(c)) continue;
     27 
     28       // console.log(`FOUND ${c} in ${compartmentA} and ${compartmentB}`);
     29       if (startA <= C && C <= stopA) {
     30         priority += 1 + C - startA;
     31       } else if (startB <= C && C <= stopB) {
     32         priority += 27 + C - startB;
     33       }
     34 
     35       break;
     36     }
     37 
     38     cb();
     39   }))
     40 
     41   .on('finish', () => {
     42     // done
     43     console.log('Priority', priority);
     44   });
     45 
     46 let groupBuffer = [];
     47 fs.createReadStream('input')
     48   .pipe(line_for_line())
     49 
     50   // Group by 3
     51   .pipe(through(function(line, enc, cb) {
     52     line = line.toString();
     53     groupBuffer.push(line);
     54     if (groupBuffer.length == 3) {
     55       this.push(JSON.stringify(groupBuffer));
     56       groupBuffer = [];
     57     }
     58     cb();
     59   }))
     60 
     61   // Business logic
     62   .pipe(through(function(line, enc, cb) {
     63     const [x,y,z] = JSON.parse(line);
     64     // Find the badge
     65 
     66     // Iterate the one, as it must have the badge
     67     for(let i=0; i<x.length; i++) {
     68 
     69       // Filter down to the badge
     70       const c = x.substr(i, 1);
     71       const C = c.charCodeAt(0);
     72       if (!~y.indexOf(c)) continue;
     73       if (!~z.indexOf(c)) continue;
     74 
     75       // console.log(`FOUND ${c} in group [${x}, ${y}, ${z}]`);
     76       if (startA <= C && C <= stopA) {
     77         priorityGroup += 1 + C - startA;
     78       } else if (startB <= C && C <= stopB) {
     79         priorityGroup += 27 + C - startB;
     80       }
     81 
     82       break;
     83     }
     84 
     85     // console.log(JSON.parse(line));
     86     cb();
     87   }))
     88 
     89 
     90   .on('finish', () => {
     91     // done
     92     console.log('PriorityGroup', priorityGroup);
     93   })