jsadvent/solutions/2015/1.js
2020-12-10 02:33:00 -06:00

20 lines
398 B
JavaScript

const { Glib } = require('../../lib');
const VALUE = {
'(': 1n,
')': -1n,
};
module.exports = {
'1': (input) =>
Glib.fromIterable(input)
.map((char) => VALUE[char] || 0n)
.sum(),
'2': (input) =>
Glib.fromIterable(input).partialReduce(
([floor], char, index) => [floor + (VALUE[char] || 0n), index + 1],
([floor]) => floor >= 0,
[0n, 0],
)[1],
};