Public
Edited
Nov 11, 2022
1 fork
1 star
Insert cell
Insert cell
function numValid(rule, input) {
const re = /(\d+)[-](\d+) (.): (.+)/;
const pwds = input.split('\n').map(s => s.match(re).slice(1));
return pwds.filter(rule).length;
}
Insert cell
Insert cell
function occurrences(c, cs) {
let n = 0;
let pos = cs.indexOf(c);

while (pos !== -1) {
n++;
pos = cs.indexOf(c, pos + 1);
}
return n;
}
Insert cell
Insert cell
function isValid([n1, n2, c, pwd]) {
const freq = occurrences(c, pwd);
return freq >= n1 && freq <= n2;
}
Insert cell
Insert cell
Insert cell
Insert cell
function isValid2([n1, n2, c, pwd]) {
const match1 = pwd.charAt(n1 - 1) == c;
const match2 = pwd.charAt(n2 - 1) == c;
return match1 ? !match2 : match2;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more