Skip to content

Commit 88fc9d1

Browse files
completed Day 2 part 2
1 parent efc06ba commit 88fc9d1

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

day2/README.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ Each line gives the password policy and then the password. The password policy i
1616

1717
In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.
1818

19-
How many passwords are valid according to their policies?
19+
How many passwords are valid according to their policies?
20+
21+
--- Part Two ---
22+
While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System is expecting.
23+
24+
The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place down the street! The Official Toboggan Corporate Policy actually works a little differently.
25+
26+
Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement.
27+
28+
Given the same example list from above:
29+
30+
1-3 a: abcde is valid: position 1 contains a and position 3 does not.
31+
1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
32+
2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
33+
How many passwords are valid according to the new interpretation of the policies?

day2/password.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import re
22

33

4-
def is_valid(password: str, policy_character, min: int, max: int) -> bool:
5-
return min <= len(re.findall(policy_character, password)) <= max
4+
def is_valid(password: str, policy_character, first: int, second: int) -> bool:
5+
return (password[first-1] == policy_character) != (password[second-1] == policy_character)
6+
67

78
def is_valid_policy(password_policy: str) -> bool:
89
policy_attributes = password_policy.split()
9-
min = int(policy_attributes[0].split("-")[0])
10-
max = int(policy_attributes[0].split("-")[1])
10+
first = int(policy_attributes[0].split("-")[0])
11+
second = int(policy_attributes[0].split("-")[1])
1112
policy_character = policy_attributes[1].replace(":", "")
1213
password = policy_attributes[2]
13-
return is_valid(password, policy_character, min, max)
14+
return is_valid(password, policy_character, first, second)
1415

1516

1617
if __name__ == '__main__':

day2/test_password.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ class MyTestCase(unittest.TestCase):
77
def test_valid_password(self):
88
policy_character = 'a'
99
user_password = 'abcde'
10-
min = 1
11-
max = 3
12-
self.assertTrue(password.is_valid(user_password, policy_character, min, max))
10+
first = 1
11+
second = 3
12+
self.assertTrue(password.is_valid(user_password, policy_character, first, second))
1313

1414
def test_invalid_password(self):
1515
policy_character = 'b'
1616
user_password = 'cdefg'
17-
min = 1
18-
max = 3
19-
self.assertFalse(password.is_valid(user_password, policy_character, min, max))
17+
first = 1
18+
second = 3
19+
self.assertFalse(password.is_valid(user_password, policy_character, first, second))
2020

2121
def test_valid_same_char(self):
2222
policy_character = 'c'
2323
user_password = 'ccccccccc'
24-
min = 2
25-
max = 9
26-
self.assertTrue(password.is_valid(user_password, policy_character, min, max))
24+
first = 2
25+
second = 9
26+
self.assertFalse(password.is_valid(user_password, policy_character, first, second))
2727

2828
def test_valid_policy(self):
2929
policy = '1-3 a: abcde'

0 commit comments

Comments
 (0)