-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.coffee
73 lines (65 loc) · 1.32 KB
/
13.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
_log = require 'ololog'
generate = ( x,y, magic )->
key = x*x + 3*x + 2*x*y + y + y*y + magic
bin = key.toString 2
ones = 0
for c in bin when c is '1'
++ones
'.#'[ones%2]
cross = [[1,0],[-1,0],[0,1],[0,-1]]
walk_around = ( start, magic, cb )->
map = {}
map[start[0]]={}
map[start[0]][start[1]]=0
next = [start]
step = 0
while next.length
++step
_log.darkGray step, next.length
cur = next
next = []
for p in cur
for d in cross
x = p[0]+d[0]
y = p[1]+d[1]
if x<0 or y<0
continue
if not map[x]?[y]?
map[x]?={}
map[x][y] = generate x,y, magic
if map[x][y] is '.'
if cb x, y, step
map[x][y] = '+'
return map
map[x][y] = step
next.push [x,y]
map
find_shortest_route = ( start, end, magic, dump_at )->
w=h=0
found = undefined
visited = 1
map = walk_around start, magic, ( x, y, step )->
++visited
#_log 'step', step, 'visited', visited, 'pos', x, y
if step is dump_at
#dump_after = Infinity
_log.green visited
w=x if w<x
h=y if h<y
if x is end[0] and y is end[1]
found = step
return yes
return
_log.cyan w, h
for y in [0..h]
_log (for x in [0..w]
(' '+(map[x][y] ? ' ').toString())[-3..]
).join ''
found
do ->
try
magic = 1352
_log.yellow find_shortest_route [1,1], [31,39], magic, 50
catch e
_log.red e
return