Skip to content

Instantly share code, notes, and snippets.

@pedromcunha
Created December 27, 2019 19:08
Show Gist options
  • Save pedromcunha/5f4f08ec9cf470195f202c31ec0fcf11 to your computer and use it in GitHub Desktop.
Save pedromcunha/5f4f08ec9cf470195f202c31ec0fcf11 to your computer and use it in GitHub Desktop.
Adent of Code 4.1
// const range = 136760-595730
const minimum = 136760
const maximum = 595730
let possiblePasswords = 0
class PasswordChecker {
check(password) {
password = password.split("")
const hasJointDuplicates = this.hasJointDuplicates(password)
if (!hasJointDuplicates) { return false }
const followsIncrementRule = this.followsIncrementRule(password)
if (!followsIncrementRule) { return false }
return true
}
hasJointDuplicates(password) {
const duplicate = password.find((character, index) => character == password[index + 1])
return duplicate != undefined
}
followsIncrementRule(password) {
for (var i = 0; i < password.length; i++) {
const character = password[i]
const nextCharacter = password[i+1]
if (nextCharacter == undefined) {
return true
}
if (character <= nextCharacter == false) {
return false
}
}
return true
}
}
const checker = new PasswordChecker()
for (var i = minimum; i < maximum + 1; i++) {
if (checker.check(i + "")) {
possiblePasswords++
}
}
console.log(possiblePasswords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment