Skip to content

Instantly share code, notes, and snippets.

@yaron-idan
Created December 4, 2019 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaron-idan/38ea4ee3d91d2330ca3285665150283e to your computer and use it in GitHub Desktop.
Save yaron-idan/38ea4ee3d91d2330ca3285665150283e to your computer and use it in GitHub Desktop.
day 04
function passwordCriteria(password) {
isValidPassword = true;
// if (Math.ceil(Math.log(password + 1) / Math.LN10) != 6){
// console.log("password is not 6 digits - "+password.length)
// isValidPassword = false;
// }
if (isValidPassword && !isAdjacentPairNew(password)){
isValidPassword = false;
}
if (isValidPassword && !isAscending(Math.floor(password/10), password % 10)){
isValidPassword = false;
}
return isValidPassword;
}
function isAdjacentPairNew(password){
isAdjacent = false;
isIdentical = false;
for (j = 0; j < 6; j++){
currDigit = password % 10;
password = Math.floor(password/10);
if (currDigit == password % 10){
if (isAdjacent || isIdentical){
isAdjacent = false
isIdentical = true;
}
else {
isAdjacent = true;
}
}
else if (isAdjacent){
return true;
}
else {
isIdentical = false;
}
}
return isAdjacent;
}
function isAdjacentPair(password, previousDigit){
currentDigit = password % 10;
if (currentDigit == previousDigit){
//console.log("found a match between - "+currentDigit+" and "+previousDigit);
return true;
}
else if (password == 0){
//console.log("digits depleted");
return false;
}
else{
//console.log("going deeper with - "+password);
return isAdjacentPair(Math.floor(password/10), currentDigit);
}
}
function isAscending(password, previousDigit){
currentDigit = password % 10;
if (currentDigit > previousDigit){
//console.log("found a match between - "+currentDigit+" and "+previousDigit);
return false;
}
else if (password == 0){
//console.log("digits depleted");
return true;
}
else{
return isAscending(Math.floor(password/10), password % 10);
}
}
let counter = 0;
for (i = 347312; i < 805915; i++) {
console.log("testing i - "+i);
if (passwordCriteria(i)){
counter += 1;
console.log(counter+" counter bump");
}
else {
console.log(counter+" counter stays the same");
}
}
console.log(counter);
module.exports = {
passwordCriteria
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment