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

34 lines
841 B
JavaScript

const { Glib } = require('../../lib');
const parse = (input) =>
Glib.fromLines(input).map((i) => {
const [meta, password] = i.split(':');
const [lens, char] = meta.split(' ');
const [min, max] = lens.split('-');
return {
min: parseInt(min, 10),
max: parseInt(max, 10),
char,
password: password.trim(),
};
});
const solve = (input, filterFn) => parse(input).filter(filterFn).length;
module.exports = {
'1': (input) =>
solve(input, ({ min, max, char, password }) => {
const charCount = password.split('').filter((c) => c === char).length;
return charCount >= min && charCount <= max;
}),
'2': (input) =>
solve(
input,
({ min, max, char, password }) =>
(password[min - 1] === char) !== (password[max - 1] === char),
),
parse,
solve,
};