DEV Community

loading...

Advent of Code (Day - 2 Password Philosophy)

harsha profile image Harshavardhan ・3 min read

If you want to solve this puzzle, visit https://adventofcode.com/2020/day/2

Puzzle

The challenge is to validate passwords

Part 1

For example, suppose you have the following list:

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
Enter fullscreen mode Exit fullscreen mode

Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times.

In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.

How many passwords are valid according to their policies?

Solution in Python

Parsing Input File

with open("Problem-2/Day-2-Password-Philosophy.txt") as file:
    for line in file:
        freq, letter, password = line.split(" ")
        letter = letter[:len(letter) - 1]
        low, high = map(int, freq.split('-'))
Enter fullscreen mode Exit fullscreen mode

Solution Part 1

def partOne(low, high, letter, password):
    count = 0
# Counting frequency of the letter
    for char in password:
        if char == letter:
            count += 1
# Password is vaild if the frequency count lies in the low-high range
    return high >= count >= low

out1 = 0
with open("Problem-2/Day-2-Password-Philosophy.txt") as file:
    for line in file:
        freq, letter, password = line.split(" ")
        letter = letter[:len(letter) - 1]
        low, high = map(int, freq.split('-'))
        if partOne(low, high, letter, password):
            out1 += 1
print(out1)
Enter fullscreen mode Exit fullscreen mode

Part 2

Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; no concept of "index zero"!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement.

Given the same example list from above:

1-3 a: abcde is valid: position 1 contains a and position 3 does not.
1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
Enter fullscreen mode Exit fullscreen mode

How many passwords are valid according to the new interpretation of the policies?

Solution Part 2

def partTwo(position1, position2, letter, password):
    return (password[position1] == letter) ^ (password[position2] == letter)
Enter fullscreen mode Exit fullscreen mode

Complete solution with Part 1 and Part 2 in Python

def partOne(low, high, letter, password):
    count = 0
    for char in password:
        if char == letter:
            count += 1
    return high >= count >= low


def partTwo(position1, position2, letter, password):
    return (password[position1] == letter) ^ (password[position2] == letter)


out1 = 0
out2 = 0
with open("Problem-2/Day-2-Password-Philosophy.txt") as file:
    for line in file:
        freq, letter, password = line.split(" ")
        letter = letter[:len(letter) - 1]
        low, high = map(int, freq.split('-'))
        if partOne(low, high, letter, password):
            out1 += 1
        if partTwo(low - 1, high - 1, letter, password):
            out2 += 1

print(out1, out2)
Enter fullscreen mode Exit fullscreen mode

Complete solution with Part 1 and Part 2 in JavaScript

const partOne = (low, high, letter, password) =>{
    let count = 0;
    for(char of password){
        if (char === letter){
            count += 1;
        }
    }
    return (count >= low && count <= high);
}

const partTwo = (low, high, letter, password) =>{
    return (password[low] === letter) ^ (password[high] === letter)
}

const fs = require('fs');
const data = fs.readFileSync('Problem-2/Day-2-Password-Philosophy.txt').toString().split("\n");
let out1 = 0, out2 = 0;
data.forEach((line)=>{
    let [freq, letter, password] = line.split(' ');
    let [low, high] = freq.split('-').map((val) => Number(val));
    letter = letter.slice(0, letter.length - 1);
    if (partOne(low, high, letter, password)){ out1++; }
    if (partTwo(low - 1, high - 1, letter, password)){ out2++; }    
})

console.log(out1, out2);
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide