conststart=123257;constend=647015;functionisMonotone(pwd){returnpwd.split('').every((digit,index)=>index===0||digit>=pwd[index-1]);}functionhasGroupOfTwo(pwd){// For part 1return/(\d)\1/.test(pwd);}functionhasGroupOfOnlyTwo(pwd){// For part 2return(pwd.match(/(\d)\1+/g)||[]).map(sequence=>sequence.length).includes(2);}functionisCandidate(pwd){returnhasGroupOfTwo(pwd)&&isMonotone(pwd);}letvalidCount=0;for(letpwd=start;pwd<=end;pwd++){validCount+=isCandidate(String(pwd));}console.log(validCount);
For the second part, replace hasGroupOfTwo with hasGroupOfOnlyTwo in isCandidate.
My solution in JavaScript, with some regex magic.
For the second part, replace
hasGroupOfTwo
withhasGroupOfOnlyTwo
inisCandidate
.Check out my solutions at github.com/MaxArt2501/advent-of-co...