-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.coffee
75 lines (64 loc) · 1.26 KB
/
17.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
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
_log = require 'ololog'
crypto = require 'crypto'
room = '''
#########
#S| | | #
#-#-#-#-#
# | | | #
#-#-#-#-#
# | | | #
#-#-#-#-#
# | | |
####### V
'''
md5 = ( str )->
crypto.createHash('md5').update(str).digest 'hex'
DIR =
U: [0,-1,0]
D: [0,1,1]
L: [-1,0,2]
R: [1,0,3]
find_path = ( code, longest )->
next = '':[0,0]
next_len = 1
step = 0
max = undefined
while next_len
++step
# if longest
# _log.darkGray 'step', step, 'len', next_len
cur = next
next = {}
next_len = 0
for k,[x,y] of cur
h = md5 code+k
for d,[dx,dy,i] of DIR when 0<=(nx=x+dx)<=3 and 0<=(ny=y+dy)<=3 and h[i] in 'bcdef'
nk = k+d
if nx is 3 and ny is 3
if longest
max = nk
else
return nk
else
next[nk]=[nx,ny]
++next_len
max
do ->
try
# _log md5 'hijkl'
# _log md5 'hijklD'
# _log md5 'hijklDR'
# _log md5 'hijklDU'
# _log md5 'hijklDUR'
_log.red find_path 'hijkl'
_log.cyan find_path 'ihgpwlah'
_log.cyan find_path 'kglvqrro'
_log.cyan find_path 'ulqzkmiv'
_log.yellow find_path 'ioramepc'
_log.cyan find_path('ihgpwlah', yes).length
_log.cyan find_path('kglvqrro', yes).length
_log.cyan find_path('ulqzkmiv', yes).length
_log.yellow find_path('ioramepc', yes).length
catch e
_log.red e
return