-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_part2.py
executable file
·43 lines (37 loc) · 1.44 KB
/
day2_part2.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python3
import loader
import re
def parse_password_line(password_line):
"""
Mandatory arguments:
* password_line - a line taken directly from day 2's input.
Example: "5-11 t: glhbttzvzttkdx"
Returns:
{"minimum": 5,
"maximum": 11,
"letter": "t",
"password": "glhbttzvzttkdx"
}
"""
matches = re.match('^([0-9]+)-([0-9]+) (.): (.+$)', password_line)
return {"minimum": int(matches.group(1)),
"maximum": int(matches.group(2)),
"letter": matches.group(3),
"password": matches.group(4) }
def is_password_valid(password_dict):
"""
Mandatory arguments:
* password_dict - a password dictionary a returned by parse_password_line
Returns True if the password is valid and False if it is not.
"""
position_1 = password_dict["password"][password_dict["minimum"]-1]
position_2 = password_dict["password"][password_dict["maximum"]-1]
if (position_1 == password_dict["letter"] and position_2 != password_dict["letter"]) or \
(position_1 != password_dict["letter"] and position_2 == password_dict["letter"]):
return True
else:
return False
password_lines = loader.string_list("input_day2")
password_dicts = [parse_password_line(password_line) for password_line in password_lines]
valid_password_count = sum([ 1 for password_dict in password_dicts if is_password_valid(password_dict) ])
print(valid_password_count)