let input = [ | |
"1-3 a: abcde", | |
"1-3 b: cdefg", | |
"2-9 c: ccccccccc" | |
] | |
// wrong | |
// function testpassword(line){ | |
// let [_, lower, upper, char, password] = line.match(/^(\d+)-(\d+)\s(\w):\s(\w+)$/) | |
// console.log(line, {lower, upper, char, password}) | |
// console.log(`${char}{${lower},${upper}}`, new RegExp(`${char}{${lower},${upper}}`).test(password)) | |
// return new RegExp(`${char}{${lower},${upper}}`).test(password) | |
// } | |
function testpassword(line){ | |
let [_, lower, upper, char, password] = line.match(/^(\d+)-(\d+)\s(\w):\s(\w+)$/) | |
let charcount = password.split("").filter(each => each == char).length | |
return lower <= charcount && charcount <= upper | |
} | |
function testpassword(line){ | |
let [_, lower, upper, char, password] = line.match(/^(\d+)-(\d+)\s(\w):\s(\w+)$/) | |
let lowerchar = password[+lower - 1] == char | |
let upperchar = password[+upper - 1] == char | |
return lowerchar && !upperchar || upperchar && !lowerchar | |
} | |
let valid = input | |
.map(testpassword) | |
.filter(Boolean) | |
.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment