This is a post with my solutions and learnings from the puzzle. Don't continue reading if you haven't tried the puzzle on your own yet.
If you want to do the puzzle, visit adventofcode.com.
My programming language of choice is
python and all examples below are in python.
- String handling
This puzzle teaches you to handle strings. They are handled differently in every programming language and it's valuable to get comfortable with them. I find that python makes it easy to work with strings, but in other languages it might require more getting used to. You are going to need string handling a lot during Advent of Code as the input file is most often a text-file.
The puzzle is to
count the number of valid passwords in your input according to a policy.
1-3 a: abcde 1-3 b: cdefg 2-9 c: ccccccccc
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.
First step is to save the input in a local file and parse it in python:
# Open the input file inputfile = open('02.input', 'r') # Parse lines data = [x.strip() for x in inputfile.readlines()]
def part1(data): total = 0 for x in data: span, letter, password = x.split() start, end = map(int, span.split('-')) count = password.count(letter) if start <= count <= end: total +=1 return total result = part1(data) print("Result part 1: %d" % result)
The second part has the same input,
but the policy is changed.
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; Toboggan Corporate Policies have 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.
def part2(data): total = 0 for x in data: span, letter, password = x.split() start, end = map(int, span.split('-')) password1 = password[start-1] == letter password2 = password[end-1] == letter # Count if either first or the other character is correct. (xor) if password1 != password2: total +=1 return total
Going for a more functional style can make the code a bit cleaner. E.g there are no mutable variables or loops in this case. Though it requires the programmer to be comfortable with
reduce functions and so on.
def validate(line): span, letter, password = x.split() start, end = map(int, span.split('-')) count = password.count(letter) return start <= count and count <= end def part1(data): valid_passwords = filter(validate, data) return len(valid_passwords)
Regex is a powerful tool to use instead of splitting the strings. In python I find it easier to split strings as I did above. But in some cases (or other languages) it can be helpful to use regex instead. Here is an example of how the validation function could look like:
import re def validate(line): start, end, letter, pw = re.search(r'(\d+)-(\d+) (\w): (\w+)', line).groups() return int(start) <= pw.count(letter) <= int(end)
As all lines have the same format
1-3 a: password I know that splitting on space them will always output an array with 3 elements. In python we can automatically destruct it:
span, letter, password = x.split() # Is same as: output = x.split(' ') span = output letter = output password = output
The map function takes two parameters; a transform-function and an array. By using
int as transform-function it will transform the strings to an integer if it's parseable.
start, end = map(int, span.split('-')) # Is same as: a, b = span.split('-') start = int(a) end = int(b)
Python has a sweet feature to allow two comparisons in this way:
if a <= b <= c: # Same as if a <= b and b <= c:
Python has a built-in function
count on strings. It takes an string as parameter and it will return the number of occurrences of that string.
s = 'aabbccaabb' count = s.count('a') # count: 4
s = 'aabcd' index = 2 char = s[index] # char: b