Advent of Code: Day #02 - Password Philosophy
5-6 minutes
Introduction
Good morning everyone! Back again for day two of the advent of code. Today we had to reverse corruption on a toboggan rentals password database.
Task One
Given an input 1-2 a: password the password is valid if the letter a appears in the password at least 1 time but at most 2. In this example the password would be valid!
Reading in the data
With all of these puzzles we need to read in the puzzle input into the program. When reading in the input I split the text up and make each line an array in the dataset. The zeroth element is an array containing the minimum and maximum allowed occurrences, the first element is the letter and the last element is the password itself. 1-2 a: password would be converted to [['1', '2'], 'a', 'password']. The code to do so can be seen below.
def read_data(path):dataset = []with open(path, "r") as file:for line in file.readlines():tmp = line.rstrip().split(":")dataset.append([tmp[0][:-2].split("-"),tmp[0][-2:][1:], tmp[1][1:]])return dataset
The Solution
To see if the letter count is between the bounds we first must count how many time the letter appears in the password. This was accomplished using the inbuilt python method count. Count is used to count words in a sentence, to make it count letters in a word I added a space between each letter to effectively make each letter of the password a word. This worked great and then it was just an if statement to verify if the count was between the allowed bounds.
def task_one(dataset):valid_count = 0for entry in dataset:min_occur = int(entry[0][0])max_occur = int(entry[0][1])letter = entry[1]password = entry[2].replace("", " ")[1: -1]# Count expects words so we add a space betweeneach character in the password to make it a 'word'occurrences = password.count(letter)if min_occur <= occurrences <= max_occur:valid_count += 1return valid_count
Task Two
The Solution
Task two reveled that in fact the two numbers are indices and for a password to be valid the letter must appear at one index but not the other, the strings are indexed started at 1. Our example from earlier is still valid because the first index doesn't equal a but the second one does 1-2 a: [p][a]ssword. This task was actually easier than the first in my opinion as it was just an xor operation checking the two indices
def task_two(dataset):valid_count = 0for entry in dataset:# Minus one because its 1 indexed not zerofirst_index = int(entry[0][0]) - 1second_index = int(entry[0][1]) - 1letter = entry[1]password = entry[2]# Convert to bool and do xorif bool(password[first_index] == letter) ^bool(password[second_index] == letter):valid_count += 1
Conclusion
One more day completed and one day closer to christmas! If you're taking part in advent of code join my leaderboard with code: 353270-1fc6ef28. Thanks to Oliver Dunk for joining already!
Links
Github Repository A link to the github repo containing all the days.
main.py A direct download of the main.py script
1.4Kb