advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit f9d8317ebea47344fb208b9ba1bf0406527dc10e
parent 32356ba5b19ce392bb6c5884b3793279b7525b68
Author: finwo <finwo@pm.me>
Date:   Tue, 15 Nov 2022 17:11:55 +0100

Made some common 2015 tools reusable

Diffstat:
A2015/common/char-for-char.js | 9+++++++++
A2015/common/line-for-line.js | 23+++++++++++++++++++++++
A2015/common/normalize-newlines.js | 23+++++++++++++++++++++++
M2015/day-01/index.js | 11++---------
M2015/day-02/index.js | 50+++-----------------------------------------------
M2015/day-03/index.js | 11++---------
M2015/package-lock.json | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M2015/package.json | 1+
8 files changed, 197 insertions(+), 65 deletions(-)

diff --git a/2015/common/char-for-char.js b/2015/common/char-for-char.js @@ -0,0 +1,9 @@ +const through = require('through2'); + +module.exports = () => through(function(chunk, enc, cb) { + chunk = chunk.toString(); + for(let i = 0; i < chunk.length; i++) { + this.push(chunk.charAt(i)); + } + cb(); +}); diff --git a/2015/common/line-for-line.js b/2015/common/line-for-line.js @@ -0,0 +1,23 @@ +const through = require('through2'); +const multipipe = require('multipipe'); +const characterize = require('./char-for-char'); +const normalize = require('./normalize-newlines.js'); + +module.exports = () => + multipipe( + characterize(), + normalize(), + through.obj(function(chunk, enc, cb) { + if (!this.buf) this.buf = Buffer.alloc(0); + if (chunk.toString() == '\n') { + this.push(this.buf); + this.buf = Buffer.alloc(0); + return cb(); + } + const buf = Buffer.alloc(this.buf.length + chunk.length); + this.buf.copy(buf, 0); + chunk.copy(buf, this.buf.length); + this.buf = buf; + cb(); + }) + ) diff --git a/2015/common/normalize-newlines.js b/2015/common/normalize-newlines.js @@ -0,0 +1,23 @@ +const through = require('through2'); + +module.exports = () => through(function(chunk, enc, cb) { + chunk = chunk.toString(); + switch(chunk) { + case '\r': + if (this.osx) { + this.push('\n'); + } + this.osx = true; + break; + default: + if (this.osx) { + if (chunk !== '\n') { + this.push('\n'); + } + this.osx = false; + } + this.push(chunk); + break; + } + cb(); +}); diff --git a/2015/day-01/index.js b/2015/day-01/index.js @@ -1,20 +1,13 @@ const fs = require('fs'); const through = require('through2'); +const char_for_char = require('../common/char-for-char'); let floor = 0; let visitedBasement = false; let ccount = 0; fs.createReadStream('input') - - // Convert to characters - .pipe(through(function(chunk, enc, cb) { - chunk = chunk.toString(); - for(let i = 0; i < chunk.length; i++) { - this.push(chunk.charAt(i)); - } - cb(); - })) + .pipe(char_for_char()) // Business logic .pipe(through(function(chunk, enc, cb) { diff --git a/2015/day-02/index.js b/2015/day-02/index.js @@ -1,61 +1,17 @@ const fs = require('fs'); const through = require('through2'); +const line_for_line = require('../common/line-for-line'); let total = 0; let ribbon = 0; fs.createReadStream('input') - - // Convert to characters - .pipe(through(function(chunk, enc, cb) { - chunk = chunk.toString(); - for(let i = 0; i < chunk.length; i++) { - this.push(chunk.charAt(i)); - } - cb(); - })) - - // Normalize newlines - .pipe(through(function(chunk, enc, cb) { - chunk = chunk.toString(); - switch(chunk) { - case '\r': - if (this.osx) { - this.push('\n'); - } - this.osx = true; - break; - default: - if (this.osx) { - if (chunk !== '\n') { - this.push('\n'); - } - this.osx = false; - } - this.push(chunk); - break; - } - cb(); - })) - - // Convert characters to lines - .pipe(through(function(chunk, enc, cb) { - if (!this.buf) this.buf = Buffer.alloc(0); - if (chunk.toString() == '\n') { - this.push(this.buf); - this.buf = Buffer.alloc(0); - return cb(); - } - const buf = Buffer.alloc(this.buf.length + chunk.length); - this.buf.copy(buf, 0); - chunk.copy(buf, this.buf.length); - this.buf = buf; - cb(); - })) + .pipe(line_for_line()) // Business logic // Convert to dimensions .pipe(through.obj(function(chunk, enc, cb) { + console.log({ chunk }); this.push(chunk.toString().split('x').map(c => parseInt(c))); cb(); })) diff --git a/2015/day-03/index.js b/2015/day-03/index.js @@ -1,20 +1,13 @@ const fs = require('fs'); const through = require('through2'); +const char_for_char = require('../common/char-for-char'); let poss = (new Array(2)).fill(0).map(() => ({ x: 0, y: 0 })); let had = [[1]]; let cnt = 1; fs.createReadStream('input') - - // Convert to characters - .pipe(through(function(chunk, enc, cb) { - chunk = chunk.toString(); - for(let i = 0; i < chunk.length; i++) { - this.push(chunk.charAt(i)); - } - cb(); - })) + .pipe(char_for_char()) // Walk through directions .pipe(through(function(chunk, enc, cb) { diff --git a/2015/package-lock.json b/2015/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@finwo/digest-md5": "^1.0.2", + "multipipe": "^4.0.0", "through2": "^4.0.2" } }, @@ -29,11 +30,78 @@ "url": "https://github.com/sponsors/finwo" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "node_modules/duplexer2": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", + "integrity": "sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==", + "dependencies": { + "readable-stream": "^2.0.2" + } + }, + "node_modules/duplexer2/node_modules/readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/duplexer2/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/duplexer2/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/multipipe": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/multipipe/-/multipipe-4.0.0.tgz", + "integrity": "sha512-jzcEAzFXoWwWwUbvHCNPwBlTz3WCWe/jPcXSmTfbo/VjRwRTfvLZ/bdvtiTdqCe8d4otCSsPCbhGYcX+eggpKQ==", + "dependencies": { + "duplexer2": "^0.1.2", + "object-assign": "^4.1.0" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "node_modules/readable-stream": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", @@ -102,11 +170,77 @@ "@finwo/digest-common": "^1.1.0" } }, + "core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "duplexer2": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", + "integrity": "sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==", + "requires": { + "readable-stream": "^2.0.2" + }, + "dependencies": { + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "multipipe": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/multipipe/-/multipipe-4.0.0.tgz", + "integrity": "sha512-jzcEAzFXoWwWwUbvHCNPwBlTz3WCWe/jPcXSmTfbo/VjRwRTfvLZ/bdvtiTdqCe8d4otCSsPCbhGYcX+eggpKQ==", + "requires": { + "duplexer2": "^0.1.2", + "object-assign": "^4.1.0" + } + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==" + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "readable-stream": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", diff --git a/2015/package.json b/2015/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@finwo/digest-md5": "^1.0.2", + "multipipe": "^4.0.0", "through2": "^4.0.2" } }