-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
83 lines (73 loc) · 1.52 KB
/
day11.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
75
76
77
78
79
80
81
82
83
import sys
import re
from utils import iterate, window, timereport
@timereport
def main():
password = sys.stdin.readline().strip()
ps = valid_passwords(password)
print('part 1: ', next(ps))
print('part 2: ', next(ps))
def valid(password, pattern=re.compile(r'([a-z])\1.*([a-z])\2')):
"""
>>> valid('hijklmmn')
False
>>> valid('abbceffg')
False
>>> valid('abbcegjk')
False
>>> valid('abcbbee')
True
"""
if any(l in password for l in 'iol'):
return False
if not pattern.search(password):
return False
for a, b, c in window(to_int_list(password), 3):
if b - a == 1 and c - b == 1:
return True
return False
def next_password(password):
"""
>>> next_password('a')
'b'
>>> next_password('xx')
'xy'
>>> next_password('xy')
'xz'
>>> next_password('xz')
'ya'
>>> next_password('z')
'aa'
"""
i = len(password) - 1
xs = to_int_list(password)
carry = 1
while carry:
if i == -1:
xs.insert(0, carry // 26)
break
xs[i] += 1
carry = xs[i] // 26
xs[i] %= 26
i -= 1
return from_int_list(xs)
def valid_passwords(start):
return filter(valid, generate_passwords(start))
def generate_passwords(start):
return iterate(next_password, start)
def to_int_list(p):
"""
>>> to_int_list('abc')
[0, 1, 2]
"""
return [ord(c) - ord('a') for c in p]
def from_int_list(xs):
"""
>>> from_int_list([0, 1, 2])
'abc'
"""
return ''.join(chr(x + ord('a')) for x in xs)
if __name__ == '__main__':
import doctest
doctest.testmod()
main()