-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpartOne.py
44 lines (32 loc) · 1.12 KB
/
partOne.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
44
import re
from common import *
def partOne(instr: str) -> int:
input_string = parse(instr)
class Password:
plaintext: str
target_letter: str
min_repeats: int
max_repeats: int
def __init__(
self, plaintext: str, target_letter: str, min_repeats: int, max_repeats: int
) -> None:
self.plaintext = plaintext
self.target_letter = target_letter
self.min_repeats = min_repeats
self.max_repeats = max_repeats
parser_regex = r"(\d+)-(\d+) ([a-z]): (.+)"
passwords = []
for line in input_string:
m = re.match(parser_regex, line)
passwords.append(
Password(m.group(4), m.group(3), int(m.group(1)), int(m.group(2)))
)
num_valid_passwords = 0
for password in passwords:
target_letter_count = 0
for char in password.plaintext:
if char == password.target_letter:
target_letter_count += 1
if password.min_repeats <= target_letter_count <= password.max_repeats:
num_valid_passwords += 1
return num_valid_passwords