Created
December 4, 2019 19:33
-
-
Save yaron-idan/38ea4ee3d91d2330ca3285665150283e to your computer and use it in GitHub Desktop.
day 04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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