33 lines
781 B
JavaScript
33 lines
781 B
JavaScript
|
const { Glib, Maps } = require('../../lib');
|
||
|
|
||
|
const parse = (input) =>
|
||
|
Glib.fromSplit(input, '\n\n').map((i) => {
|
||
|
return Glib.fromSplit(i)
|
||
|
.map((i) => i.characterFrequencies)
|
||
|
.reduce(
|
||
|
({ length, frequencies }, b) => ({
|
||
|
length: length + BigInt(1),
|
||
|
frequencies: Maps.add(frequencies, b),
|
||
|
}),
|
||
|
{ length: BigInt(0), frequencies: new Map() },
|
||
|
);
|
||
|
});
|
||
|
|
||
|
module.exports = {
|
||
|
'1': (input) => {
|
||
|
return parse(input)
|
||
|
.map(({ frequencies }) => {
|
||
|
return BigInt(frequencies.size);
|
||
|
})
|
||
|
.sum();
|
||
|
},
|
||
|
'2': (input) => {
|
||
|
return parse(input)
|
||
|
.map(
|
||
|
({ length, frequencies }) =>
|
||
|
new Glib(frequencies.values()).filter((i) => i === length).length,
|
||
|
)
|
||
|
.sum();
|
||
|
},
|
||
|
};
|