commit 9983339ef40f385a54274bf728c7a302066f39a5
parent 9686014fd23301fdacd9c599d1fa545e5c800374
Author: finwo <finwo@pm.me>
Date: Thu, 1 Dec 2022 11:43:26 +0100
2015/05 solution
Diffstat:
| A | 2015/day-05/index.js | | | 94 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 94 insertions(+), 0 deletions(-)
diff --git a/2015/day-05/index.js b/2015/day-05/index.js
@@ -0,0 +1,94 @@
+const fs = require('fs');
+const through = require('through2');
+const line_for_line = require('../common/line-for-line');
+
+let remainder = 0;
+
+fs.createReadStream('input')
+ .pipe(line_for_line())
+
+ // Convert to string
+ .pipe(through(function(line, enc, cb) {
+ this.push(line.toString());
+ cb();
+ }))
+
+ // // Bad if invalid combinations are found
+ // .pipe(through(function(line, enc, cb) {
+ // line = line.toString();
+ // const badStrings = [
+ // 'ab',
+ // 'cd',
+ // 'pq',
+ // 'xy',
+ // ];
+ // for(const badString of badStrings) {
+ // if (~line.indexOf(badString)) {
+ // return cb();
+ // }
+ // }
+ // this.push(line);
+ // cb()
+ // }))
+
+ // // Containing less than 3 vowels = not nice
+ // .pipe(through(function(line, enc, cb) {
+ // line = line.toString();
+ // const vowels = (line.match(/[aeiou]/g) || []).length;
+ // if (vowels < 3) {
+ // return cb();
+ // }
+ // this.push(line);
+ // cb();
+ // }))
+
+ // // Must have 2 of the same characters in a row
+ // .pipe(through(function(line, enc, cb) {
+ // line = line.toString();
+ // const matches = (line.match(/([a-z])\1+/g) || []).length;
+ // if (!matches) {
+ // return cb();
+ // }
+ // this.push(line);
+ // cb();
+ // }))
+
+ // Must match ?.?, where ? is the same
+ .pipe(through(function(line, enc, cb) {
+ line = line.toString();
+ const matches = (line.match(/([a-z]).\1/g) || []).length;
+ if (!matches) {
+ return cb();
+ }
+ this.push(line);
+ cb();
+ }))
+
+ // Must have non-overlapping character pair
+ .pipe(through(function(line, enc, cb) {
+ line = line.toString();
+ let matched = false;
+ for(let i=0; i<(line.length-1); i++) {
+ const set = line.substr(i, 2);
+ const idx = line.indexOf(set);
+ if ((idx >= 0) && (idx < (i-1))) {
+ matched = true;
+ break;
+ }
+ }
+ if (!matched) {
+ return cb();
+ }
+ this.push(line);
+ cb();
+ }))
+
+ .pipe(through(function(line, enc, cb) {
+ remainder++;
+ this.push(line);
+ cb();
+ }))
+
+ .on('finish', () => {
+ console.log('finished', remainder);
+ })