fix bigInt usage

This commit is contained in:
Kegan Myers 2020-12-13 00:52:00 -06:00
parent 1c618df84c
commit 1a5bb7c360
5 changed files with 30 additions and 5 deletions

View file

@ -89,7 +89,7 @@ class Glib {
flatMap(mapFn) { flatMap(mapFn) {
return new Glib( return new Glib(
(function*(iterable) { (function*(iterable) {
let index = 0; let index = 0n;
for (const entry of iterable) { for (const entry of iterable) {
yield* mapFn(entry, index++); yield* mapFn(entry, index++);
} }
@ -330,7 +330,7 @@ class Glib {
); );
} }
reduceScoreK(chooseFn) { 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() { minScoreK() {
return this.reduceScoreK((a, b) => a < b); return this.reduceScoreK((a, b) => a < b);
@ -338,6 +338,15 @@ class Glib {
maxScoreK() { maxScoreK() {
return this.reduceScoreK((a, b) => a > b); 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; module.exports = Glib;

View file

@ -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) { Array.prototype.safeSplice = function safeSplice(...args) {
const spliced = this.slice(); const spliced = this.slice();
spliced.splice(...args); spliced.splice(...args);

View file

@ -1,5 +1,13 @@
const crypto = require('crypto'); const crypto = require('crypto');
Object.defineProperty(String.prototype, 'lengthN', {
enumerable: false,
configurable: false,
get() {
return BigInt(this.length);
},
});
Object.defineProperty(String.prototype, 'md5', { Object.defineProperty(String.prototype, 'md5', {
enumerable: false, enumerable: false,
configurable: false, configurable: false,

View file

@ -8,14 +8,14 @@ const VALUE = {
module.exports = { module.exports = {
'1': (input) => '1': (input) =>
Glib.fromIterable(input) Glib.fromIterable(input)
.filter((char, index) => char === input[(index + 1) % input.length]) .filter((char, index) => char === input[(index + 1n) % input.lengthN])
.toInts() .toInts()
.sum(), .sum(),
'2': (input) => '2': (input) =>
Glib.fromIterable(input) Glib.fromIterable(input)
.filter( .filter(
(char, index) => (char, index) =>
char === input[(index + input.length / 2) % input.length], char === input[(index + input.lengthN / 2n) % input.lengthN],
) )
.toInts() .toInts()
.sum(), .sum(),

View file

@ -23,7 +23,7 @@ module.exports = {
...Glib.fromLines(input).map((line) => ...Glib.fromLines(input).map((line) =>
twodee twodee
.everyStep(parseLine(line)) .everyStep(parseLine(line))
.map((pt, i) => [pt.string, i + 1]) .map((pt, i) => [pt.string, i + 1n])
.toMap(), .toMap(),
), ),
), ),