-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.coffee
49 lines (40 loc) · 884 Bytes
/
11.coffee
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
_log = console.log.bind console
input = 'vzbxkghb'
alpha = 'abcdefghijklmnopqrstuvwxyz'
CHARS = 'abcdefghjkmnpqrstuvwxyz'
BASE = CHARS.length
CODES = {}
CODES[c] = i for c, i in CHARS
decode = ( pwd )->
pwd.split('').map (c)->CODES[c]
encode = ( codes )->
codes.map (i)->CHARS[i]
.join ''
is_valid = ( pwd )->
straight = no
pairs = {}
for c, i in pwd
straight = yes if c+1 is pwd[i+1] and pwd[i+1]+1 is pwd[i+2]
pairs[c]=1 if c is pwd[i+1]
straight and Object.keys(pairs).length>=2
inc = ( pwd )->
for i in [pwd.length-1..0]
if pwd[i]<BASE-1
pwd[i]++
return
else
pwd[i]=0
throw new Error 'overflow'
return
next_pwd = ( pwd )->
codes = decode pwd
i = 0
while yes
inc codes
++i
_log i unless i%100
break if is_valid codes
encode codes
_log input, decode input
_log (pwd = next_pwd input), decode pwd
_log (pwd = next_pwd pwd), decode pwd