|
|
@@ -0,0 +1,46 @@
|
|
|
+
|
|
|
+const fs = require('fs');
|
|
|
+const readline = require('readline');
|
|
|
+
|
|
|
+function compareArray(a, b) {
|
|
|
+ for (let i =0; i < a.length; ++i) {
|
|
|
+ let comp = compare(a[i], b[i]);
|
|
|
+ if (comp !== 0) return comp;
|
|
|
+ }
|
|
|
+ return a.length === b.length ? 0 : -1;
|
|
|
+}
|
|
|
+
|
|
|
+function compare(a, b) {
|
|
|
+ if (b === undefined) return 1;
|
|
|
+ if (Array.isArray(a) || Array.isArray(b))
|
|
|
+ return compareArray(Array.isArray(a) ? a : [a], Array.isArray(b) ? b : [b]);
|
|
|
+ if (a < b) return -1;
|
|
|
+ if (a > b) return 1;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+async function main() {
|
|
|
+ let allLines = [[[2]], [[6]]];
|
|
|
+ let lines = [];
|
|
|
+ let index = 1;
|
|
|
+ let total = 0;
|
|
|
+ for await (let line of readline.createInterface({ input: process.stdin })) {
|
|
|
+ if (line.length) {
|
|
|
+ const data = JSON.parse(line);
|
|
|
+ lines.push(data);
|
|
|
+ allLines.push(data);
|
|
|
+ } else {
|
|
|
+ if (compare(lines[0], lines[1]) > 0)
|
|
|
+ total += index;
|
|
|
+ ++index;
|
|
|
+ lines = [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ allLines = allLines.sort(compare);
|
|
|
+ console.log("Part 1: ", total);
|
|
|
+ console.log("Part 2: ", (allLines.findIndex(i => compare(i, [[2]]) === 0) +1) *
|
|
|
+ (allLines.findIndex(i => compare(i, [[6]]) === 0) +1));
|
|
|
+};
|
|
|
+
|
|
|
+(main());
|
|
|
+
|