-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartTwo.py
More file actions
50 lines (38 loc) · 1.28 KB
/
Copy pathpartTwo.py
File metadata and controls
50 lines (38 loc) · 1.28 KB
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
45
46
47
48
49
50
import re
from common import *
def partTwo(instr: str) -> int:
input_string = parse(instr)
class Password:
plaintext: str
target_letter: str
position_one: int
position_two: int
def __init__(
self,
plaintext: str,
target_letter: str,
position_one: int,
position_two: int,
) -> None:
self.plaintext = plaintext
self.target_letter = target_letter
self.position_one = position_one - 1 # No concept of index zero... eurgh
self.position_two = position_two - 1
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:
position_one_matches = (
password.plaintext[password.position_one] == password.target_letter
)
position_two_matches = (
password.plaintext[password.position_two] == password.target_letter
)
if position_one_matches ^ position_two_matches:
num_valid_passwords += 1
return num_valid_passwords