-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle.js
28 lines (24 loc) · 829 Bytes
/
puzzle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const validPasswordCounter = (passwordList) => {
return passwordList.filter((pass) => isValidPassword(pass)).length;
};
const isValidPassword = (passwordCondition) => {
const { min, max, character, password } =
parsePasswordCondition(passwordCondition);
const occurences = password.match(new RegExp(character, "g"))?.length || 0;
return occurences >= min && occurences <= max;
};
const parsePasswordCondition = (passwordCondition) => {
const [range, character, password] = passwordCondition
.split(" ")
.filter(Boolean);
return {
min: parseInt(range.split("-")[0]),
max: parseInt(range.split("-")[1]),
character: character[0],
password,
};
};
module.exports = (input) => {
const sanitizedInput = input.split("\n").filter(Boolean);
return validPasswordCounter(sanitizedInput);
};