summaryrefslogtreecommitdiff
path: root/day2/day2.py
blob: 4c68e632e9ea248ef984ecb76507187678d0912d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import re
from IPython import embed

data = [str.strip(x) for x in open("input")]

pattern = r"(\d+)-(\d+) ([a-z]): ([a-z]+)"
prog = re.compile(pattern)

# Part 1

valid_count = 0
for x in data:
    low, high, char, password = prog.match(x).groups()
    if int(low) <= password.count(char) <= int(high):
        valid_count += 1
print(valid_count)

# Part 2

valid_count = 0
for x in data:
    low, high, char, password = prog.match(x).groups()
    if [password[int(low) - 1], password[int(high) - 1]].count(char) == 1:
        valid_count += 1
print(valid_count)