Day 2! Advent of Code! This one makes my security engineer side cringe ;P In this one... We're looking at the worst password policies imaginable - Day 2 sets us up for doing some password debugging. The link for what day 2 entails is here:
https://adventofcode.com/2020/day/2
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
let correctPasswords = 0;
lines.forEach(line =>{
// Create regex groups for the password policy
const regexGroups = /^(?<min>\d+)-(?<max>\d+) (?<policy>.): (?<password>.*)$/.exec(line);
let counter = 0;
// For each character in the password, if it matches the policy, increase counter total
[regexGroups.groups.password].forEach(charCount => {
for (let i = 0; i < regexGroups.groups.password.length; i++) {
if (regexGroups.groups.password[i] == regexGroups.groups.policy){
counter++
}
}
});
// If total is in bounds, increase correct passwords
if(counter >= regexGroups.groups.min && counter <= regexGroups.groups.max){
correctPasswords++
}
})
So, above is my code for this challenge. I decided to regex out the password min, max, the character to match and the password. For each password, recursively go through each character in the password, if it matches the policy, increase a counter. If the counter is within the min & max, increase the total of correct passwords.
Part 2
For part 2, the shopkeeper realizes he's remembering the wrong awful password policy and instead each policy describes two positions in the password, with the two numbers being positions in the password that must have the policy character in exactly one of the positions. Example:
1-3 a: abcde
is valid: position1
containsa
and position3
does not.1-3 b: cdefg
is invalid: neither position1
nor position3
containsb
.2-9 c: ccccccccc
is invalid: both position2
and position9
containc
.
correctPasswords = 0;
lines.forEach(line =>{
// Create regex groups for the password policy
const regexGroups = /^(?<pos1>\d+)-(?<pos2>\d+) (?<policy>.): (?<password>.*)$/.exec(line);
let counter = 0;
let policy = regexGroups.groups.policy;
// For each character in the password, if it matches the policy, increase counter total
[regexGroups.groups.password].forEach(charCount => {
for (let i = 0; i < regexGroups.groups.password.length; i++) {
//Janky Method to get past index 0
let k = i + 1
//If index matches position and characters are the same, you shall pass!
if (((k == regexGroups.groups.pos1) || (k == regexGroups.groups.pos2)) && (regexGroups.groups.password[i] === regexGroups.groups.policy)){
counter++;
}
else{
console.log("You shall not pass!");
}
}
});
// If total is in bounds, increase correct passwords
if(counter == 1){
correctPasswords++
}
})
So this is a bit more of a cluster fuck of code, I do admit but it gets the job done. We do the same regex, this time with the numbers being labelled pos 1 and pos 2. We do the same recursion through each password and the length of the password. We have variable k which acts as a way to get past index 0 by being i + 1 - If k matches pos 1 or pos2 and the character is equal to the policy, increase the counter. At the end, we simply change our if statement to only be called upon if the counter is exactly equal to 1.
That's day 2 folks! Day 3 looks fun. See you later!