Validity fluidity

This commit is contained in:
Dan Buch 2021-01-03 20:08:16 -05:00
parent 9128f6bdc4
commit 17c3ee9316
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

View File

@ -16,20 +16,23 @@ def main() -> int:
class Policy:
def __init__(self, char: str, min_count: int, max_count: int):
def __init__(self, char: str, pos1: int, pos2: int):
self.char = char
self.min_count = min_count
self.max_count = max_count
self.pos1 = pos1
self.pos2 = pos2
@classmethod
def fromstring(cls, input_string) -> "Policy":
parts = [s.strip() for s in input_string.split(" ")][:2]
min_max = [int(s) for s in parts[0].split("-")][:2]
return cls(parts[1], min_max[0], min_max[1])
pos = [int(s) for s in parts[0].split("-")][:2]
return cls(parts[1], pos[0], pos[1])
def is_valid(self, password: str) -> bool:
actual = password.count(self.char)
return actual >= self.min_count and actual <= self.max_count
matches = 0
for pos in (self.pos1 - 1, self.pos2 - 1):
if password[pos] == self.char:
matches += 1
return matches == 1
class PolicyPassword: