Skip to content

Latest commit

 

History

History

Day 02

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

🎄 Advent of Code 2020: Day 2 🎄

Read the statement before watching the solution.

Get your puzzle input.

Part 1

In Part 1, we were asked to parse the list of passwords and count the number of those that are valid according to the corporate policy.

The input data is in the following format:

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.

In the above example, 2 passwords are valid.

To parse the input, we can split each line by whitespace. Then we can get boundaries by splitting them by - and the target letter by removing : at the end. This is a possible implementation:

answer = 0

with open("input.txt") as file:
    for line in file:
        boundaries, charecter, string = line.split()

        lowest, highest = map(int, boundaries.split('-'))
        charecter = charecter.rstrip(':')

        if lowest <= string.count(charecter) <= highest:
            answer += 1

print(answer)
560
Execution time: 1ms

Part 2

In Part 2, we were asked to parse the same input, but now exactly one of these positions must contain the given letter. So we can change only the if part. The code below uses the ^ operator, which stands for the boolean XOR.

answer = 0

with open("input.txt") as file:
    for line in file:
        boundaries, charecter, string = line.split()

        lowest, highest = map(int, boundaries.split('-'))
        charecter = charecter.rstrip(':')

        if (string[lowest - 1] == charecter) ^ (string[highest - 1] == charecter):
            answer += 1

print(answer)
303
Execution time: 1ms