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) {
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;

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

View file

@ -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,

View file

@ -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(),

View file

@ -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(),
),
),