-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.py
74 lines (66 loc) · 1.69 KB
/
11.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 1. Passwords must include one increasing straight of at least three letters,
#like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn't count.
def req1(x):
if len(x) < 3:
return False
if ord(x[0]) - ord(x[1]) == -1 and ord(x[1]) - ord(x[2]) == -1:
return True
else:
return req1(x[1:])
# 2. Passwords may not contain the letters i, o, or l, as these letters can be
#mistaken for other characters and are therefore confusing.
def req2(x):
return (
'i' not in x and
'o' not in x and
'l' not in x
)
# 3. Passwords must contain at least two different, non-overlapping pairs of
#letters, like aa, bb, or zz.
def req3(x):
count = 0
overlap = False
for i in range(0,len(x)-1):
if overlap:
overlap = False
continue
if x[i] == x[i+1]:
count += 1
overlap = True
return count >= 2
def incr(x):
if len(x) == 0:
return ''
elif x[-1] == 'z':
return incr(x[:-1]) + 'a'
else:
return x[:-1] + chr(1 + ord(x[-1]))
data = 'cqjxjnds'
while(True):
data = incr(data)
if (
req2(data) and
req1(data) and
req3(data)
):
break
print('Part 1: %s' % data)
def smart_incr(x):
if len(x) == 0:
return ''
elif x[-1] == 'z':
return incr(x[:-1]) + 'a'
else:
new_char = chr(1 + ord(x[-1]))
if new_char in ['o', 'i', 'l']:
new_char = chr(1 + ord(x[-1]))
return x[:-1] + new_char
while(True):
data = smart_incr(data)
if (
req2(data) and
req1(data) and
req3(data)
):
break
print('Part 2: %s' % data)