-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
57 lines (44 loc) · 1.37 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
import re
class password:
def __init__(self, pw):
self.pw = [ord(c)-ord('a') for c in pw]
# validation
alpha = 'abcdefghijklmnopqrstuvwxyz'
self._runs = list(''.join(run) for run in zip(alpha, alpha[1:], alpha[2:]))
self._bad = [ord(c)-ord('a') for c in 'iol']
def increment(self):
carry = True
p = 1
while carry:
self.pw[-p] += 1
if self.pw[-p] == 26:
self.pw[-p] = 0
p += 1
else:
carry = False
return self.pw
def __str__(self):
return ''.join(chr(ord('a')+c) for c in self.pw)
def validate(self):
if any(c in self.pw for c in self._bad):
#print(self, 'rejected: contains iol')
return False
if not any(r in str(self) for r in self._runs):
#print(self, 'rejected: no runs')
return False
if not re.search(r'([a-z])\1.*([a-z])\2', str(self)):
#print(self, 'rejected: not eough pairs')
return False
print(self, 'valid!')
return True
if __name__ == '__main__':
inputs = [line.rstrip('\n') for line in open(sys.argv[1])]
# p1
pw = password(inputs[0])
while not pw.validate():
pw.increment()
# p2
pw.increment()
while not pw.validate():
pw.increment()