2020: d02: ex2: add solution
This commit is contained in:
parent
28ee1e006d
commit
04329c9fd3
43
2020/d02/ex2/ex2.py
Executable file
43
2020/d02/ex2/ex2.py
Executable file
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
|
||||
@dataclass
|
||||
class Policy:
|
||||
min: int
|
||||
max: int
|
||||
letter: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Password:
|
||||
policy: Policy
|
||||
password: str
|
||||
|
||||
|
||||
def is_valid(pwd: Password) -> bool:
|
||||
min = pwd.password[pwd.policy.min - 1] == pwd.policy.letter
|
||||
max = pwd.password[pwd.policy.max - 1] == pwd.policy.letter
|
||||
return min ^ max
|
||||
|
||||
|
||||
def solve(passwords: List[Password]) -> int:
|
||||
return sum(map(is_valid, passwords))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
pattern = re.compile("([0-9]+)-([0-9]+) (.): (.+)")
|
||||
input = [
|
||||
Password(Policy(int(m.group(1)), int(m.group(2)), m.group(3)), m.group(4))
|
||||
for m in (pattern.match(line) for line in sys.stdin.readlines())
|
||||
if m
|
||||
]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue