commit b7f551c0c23d3769430e1f36de8b8afd60872197
parent c839f6de1619258ebeffd960290afd4cbf10de4c
Author: finwo <finwo@pm.me>
Date: Tue, 6 Dec 2022 07:15:27 +0100
2022/06 solution
Diffstat:
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/2022/day-06/index.js b/2022/day-06/index.js
@@ -0,0 +1,45 @@
+const fs = require('fs');
+const through = require('through2');
+const char_for_char = require('../common/char-for-char.js');
+
+const histbuf = [];
+let idx = 0;
+let L = 14;
+
+function uniq(arr) {
+ return arr.filter((v, i, a) => a.indexOf(v) == i);
+}
+
+fs.createReadStream('input')
+ .pipe(char_for_char())
+
+ // Business logic
+ .pipe(through(function(char, enc, cb) {
+ char = char.toString();
+
+ // Append character
+ histbuf.push(char);
+ idx++;
+
+ // Bail if not enough characters
+ if (histbuf.length < L) {
+ return cb();
+ }
+
+ // Limit length
+ if (histbuf.length > L) {
+ histbuf.shift();
+ }
+
+ // Check for all unique characters
+ if (uniq(histbuf).length == L) {
+ console.log('START', idx);
+ }
+
+ // console.log({ histbuf, uniq: uniq(histbuf) });
+ cb();
+ }))
+
+ .on('finish', () => {
+ // done
+ });