jsadvent/solutions/2020/6.js

33 lines
781 B
JavaScript
Raw Normal View History

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