jsadvent/solutions/2015/1.js

20 lines
398 B
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
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],
};