diff --git a/lib/Glib.js b/lib/Glib.js index 1b50e87..0248048 100644 --- a/lib/Glib.js +++ b/lib/Glib.js @@ -89,7 +89,7 @@ class Glib { flatMap(mapFn) { return new Glib( (function*(iterable) { - let index = 0; + let index = 0n; for (const entry of iterable) { yield* mapFn(entry, index++); } @@ -330,7 +330,7 @@ class Glib { ); } reduceScoreK(chooseFn) { - return this.reduce((a, b) => (chooseFn(a[1] > b[1]) ? a : b))[0]; + return this.reduce((a, b) => (chooseFn(a[1], b[1]) ? a : b))[0]; } minScoreK() { return this.reduceScoreK((a, b) => a < b); @@ -338,6 +338,15 @@ class Glib { maxScoreK() { return this.reduceScoreK((a, b) => a > b); } + reduceScore(chooseFn) { + return this.reduce((a, b) => (chooseFn(a[1], b[1]) ? a : b)); + } + minScore() { + return this.reduceScore((a, b) => a < b); + } + maxScoreK() { + return this.reduceScore((a, b) => a > b); + } } module.exports = Glib; diff --git a/lib/_pollute/array.js b/lib/_pollute/array.js index d28f430..87aa824 100644 --- a/lib/_pollute/array.js +++ b/lib/_pollute/array.js @@ -16,6 +16,14 @@ Object.defineProperty(Array.prototype, 'set', { }, }); +Object.defineProperty(Array.prototype, 'lengthN', { + enumerable: false, + configurable: false, + get() { + return BigInt(this.length); + }, +}); + Array.prototype.safeSplice = function safeSplice(...args) { const spliced = this.slice(); spliced.splice(...args); diff --git a/lib/_pollute/string.js b/lib/_pollute/string.js index b0f9b63..c88596d 100644 --- a/lib/_pollute/string.js +++ b/lib/_pollute/string.js @@ -1,5 +1,13 @@ const crypto = require('crypto'); +Object.defineProperty(String.prototype, 'lengthN', { + enumerable: false, + configurable: false, + get() { + return BigInt(this.length); + }, +}); + Object.defineProperty(String.prototype, 'md5', { enumerable: false, configurable: false, diff --git a/solutions/2017/1.js b/solutions/2017/1.js index c2932cc..3c2bbbf 100644 --- a/solutions/2017/1.js +++ b/solutions/2017/1.js @@ -8,14 +8,14 @@ const VALUE = { module.exports = { '1': (input) => Glib.fromIterable(input) - .filter((char, index) => char === input[(index + 1) % input.length]) + .filter((char, index) => char === input[(index + 1n) % input.lengthN]) .toInts() .sum(), '2': (input) => Glib.fromIterable(input) .filter( (char, index) => - char === input[(index + input.length / 2) % input.length], + char === input[(index + input.lengthN / 2n) % input.lengthN], ) .toInts() .sum(), diff --git a/solutions/2019/3.js b/solutions/2019/3.js index 33b6885..6512b3e 100644 --- a/solutions/2019/3.js +++ b/solutions/2019/3.js @@ -23,7 +23,7 @@ module.exports = { ...Glib.fromLines(input).map((line) => twodee .everyStep(parseLine(line)) - .map((pt, i) => [pt.string, i + 1]) + .map((pt, i) => [pt.string, i + 1n]) .toMap(), ), ),