2020-12-02 13:07:57 +01:00
|
|
|
from typing import List
|
|
|
|
from os.path import join, dirname
|
|
|
|
|
|
|
|
|
|
|
|
def checkRow(row: str, alternative=False) -> bool:
|
|
|
|
rule, password = map(lambda s: s.strip(), row.split(':'))
|
|
|
|
amount, char = rule.split(' ')
|
|
|
|
minimum, maximum = map(int, amount.split('-'))
|
|
|
|
if alternative:
|
2020-12-02 13:14:40 +01:00
|
|
|
return (password[minimum - 1] == char) ^ (password[maximum - 1] == char)
|
2020-12-02 13:07:57 +01:00
|
|
|
else:
|
|
|
|
occurrences = password.count(char)
|
|
|
|
return minimum <= occurrences <= maximum
|
|
|
|
|
|
|
|
|
|
|
|
data = join(dirname(__file__), 'data.txt')
|
|
|
|
with open(data) as f:
|
|
|
|
valid = 0
|
|
|
|
valid_alt = 0
|
|
|
|
rows = list(f.read().strip().split('\n'))
|
|
|
|
for row in rows:
|
|
|
|
if(checkRow(row)):
|
|
|
|
valid += 1
|
|
|
|
if(checkRow(row, alternative=True)):
|
|
|
|
valid_alt += 1
|
|
|
|
print(f'Found {valid} valid passwords.')
|
|
|
|
print('Policy changed...')
|
|
|
|
print(f'Found {valid_alt} valid passwords.')
|