commit 0d285583cd7eb162ccef53ae20164af6685ac9d3
parent cff8a8f417c906500cd267920d5934c4b15c60bd
Author: finwo <finwo@pm.me>
Date: Tue, 13 Dec 2022 12:23:38 +0100
2022/13 solution
Diffstat:
3 files changed, 145 insertions(+), 0 deletions(-)
diff --git a/2022/common/by-paragraph.js b/2022/common/by-paragraph.js
@@ -0,0 +1,24 @@
+const through = require('through2');
+const multipipe = require('multipipe');
+const line_for_line = require('./line-for-line');
+
+module.exports = () =>
+ multipipe(
+ line_for_line(),
+ through.obj(function(line, enc, cb) {
+ line = line.toString();
+ this.buffer = this.buffer || [];
+
+ // Emit paragraph on empty line
+ if (!line.length) {
+ if (this.buffer.length) {
+ this.push(this.buffer.join('\n'));
+ }
+ this.buffer = [];
+ return cb();
+ }
+
+ this.buffer.push(line);
+ cb();
+ })
+ )
diff --git a/2022/day-13/index.js b/2022/day-13/index.js
@@ -0,0 +1,97 @@
+const fs = require('fs');
+const through = require('through2');
+const by_paragraph = require('../common/by-paragraph');
+const line_by_line = require('../common/line-for-line');
+
+String.prototype.startsWith = function(subject) {
+ return this.substr(0, subject.length) === subject;
+};
+
+function verify(left, right) {
+ // If both values are
+ if (
+ ('number' === typeof left) &&
+ ('number' === typeof right)
+ ) {
+ if (left < right) return true;
+ if (left > right) return false;
+ }
+ // If both values are lists, compare values of each list
+ if (
+ Array.isArray(left) &&
+ Array.isArray(right)
+ ) {
+ const len = Math.max(left.length, right.length);
+ for(let i=0; i < len; i++) {
+ if ('undefined' === typeof left[i]) return true;
+ if ('undefined' === typeof right[i]) return false;
+ const result = verify(left[i], right[i]);
+ if ('boolean' === typeof result) return result;
+ }
+ }
+ // If exactly one value is an int, convert the integer to a list and retry
+ if (
+ (Array.isArray(left)) &&
+ ('number' === typeof right)
+ ) {
+ right = [right];
+ return verify(left, right);
+ }
+ if (
+ ('number' === typeof left) &&
+ (Array.isArray(right))
+ ) {
+ left = [left];
+ return verify(left, right);
+ }
+}
+
+let pairs = 0;
+let validSum = 0;
+const dividerA = [[2]];
+const dividerB = [[6]];
+const packets = [
+ dividerA,
+ dividerB,
+];
+
+fs.createReadStream('input')
+ .pipe(by_paragraph())
+
+ // Minor parsing
+ .pipe(through.obj(function(paragraph, enc, cb) {
+ paragraph = paragraph.toString();
+ this.push(paragraph.split('\n').map(line => JSON.parse(line)));
+ cb();
+ }))
+
+ .pipe(through.obj(function(pair, enc, cb) {
+ const [left, right] = pair;
+
+ const valid = verify(left, right);
+ pairs++;
+ if (valid) validSum += pairs;
+
+ packets.push(left, right);
+
+ console.log(left);
+ console.log(right);
+ console.log(valid);
+ console.log('');
+
+ cb();
+ }))
+
+ .on('finish', () => {
+
+ packets.sort((left, right) => {
+ return verify(left, right) ? -1 : 1;
+ });
+ const idxA = packets.indexOf(dividerA) + 1;
+ const idxB = packets.indexOf(dividerB) + 1;
+ const key = idxA * idxB;
+
+ console.log(packets);
+ console.log({ validSum, key });
+ console.log('finish');
+ });
diff --git a/2022/day-13/sample b/2022/day-13/sample
@@ -0,0 +1,24 @@
+[1,1,3,1,1]
+[1,1,5,1,1]
+
+[[1],[2,3,4]]
+[[1],4]
+
+[9]
+[[8,7,6]]
+
+[[4,4],4,4]
+[[4,4],4,4,4]
+
+[7,7,7,7]
+[7,7,7]
+
+[]
+[3]
+
+[[[]]]
+[[]]
+
+[1,[2,[3,[4,[5,6,7]]]],8,9]
+[1,[2,[3,[4,[5,6,0]]]],8,9]
+