A terrible idea oh no

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

50
aoc2020/day02/solution.py Normal file
View File

@ -0,0 +1,50 @@
import sys
def main() -> int:
n_valid = 0
total = 0
for pol_pas in [PolicyPassword.fromstring(s) for s in sys.stdin.readlines(False)]:
if pol_pas.is_valid():
n_valid += 1
total += 1
print(f"{n_valid}/{total} valid")
return 0
class Policy:
def __init__(self, char: str, min_count: int, max_count: int):
self.char = char
self.min_count = min_count
self.max_count = max_count
@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])
def is_valid(self, password: str) -> bool:
actual = password.count(self.char)
return actual >= self.min_count and actual <= self.max_count
class PolicyPassword:
def __init__(self, policy: "Policy", password: str):
self.policy = policy
self.password = password
@classmethod
def fromstring(cls, input_string: str) -> "PolicyPassword":
parts = [s.strip() for s in input_string.split(":")][:2]
return cls(Policy.fromstring(parts[0]), parts[1])
def is_valid(self) -> bool:
return self.policy.is_valid(self.password)
if __name__ == "__main__":
sys.exit(main())