View difference between Paste ID: xa5cgLwF and rBmYn6R1
SHOW: | | - or go back to the newest paste.
1-
simpleDeltas =: (1 0),(0 _1),(_1 0),,:(0 1)
1+
NB. true for rows in x which index true value in y
2-
eightdirDeltas =: (_1 1),(_1 _1),(1 _1),(1 1),simpleDeltas
2+
validIn =: (<"1@(|~ #) { ]) *. (|~ #) -:"1 [  
3
4-
indexOutOfBounds =: (0 > ]) +./@:, <: NB. x={vect}shapeOfArray, y={vect}index; z=bit
4+
filter =: 1 : '#~ u' NB. removes all elements of y where u(y[i]) is false
5
6-
atIndex =: ({~ <@:|.)"_ 1 NB. equivalent of x[y2][...][y0] in C-style language
6+
7
manhattanDeltas=: 3 :'(,-)=i.#$y' NB. unit vectors oriented on coordinate axes
8-
filter =: 1 : '((0~:u)"_1 y) # y' NB. removes all elements of y where u(y[i]) = 0
8+
9
NB. x = [[bit]]  valid squares
10
NB. y = [num]    index
11
NB. z = [[num]]  neighbors
12-
shuffle =: (#?#) { ]
12+
gridNeighbors =: 1 : 0
13
    [: validIn&m filter (manhattanDeltas m) +"1 ]
14
)
15
16
NB. m = [[bit]]        valid squares
17-
gridNeighbors =: 4 : 0
17+
18-
	neighbors =. simpleDeltas +"1 y
18+
19-
	neighbors =. (-.@(($x)&indexOutOfBounds)) filter neighbors NB. remove those leaving the grid
19+
20-
	neighbors =. x&atIndex filter neighbors NB. remove those entering a wall
20+
    > <@,"_ 1 m gridNeighbors@{:@>
21
)
22
23-
NB. Path<T> =: [{boxed num}distance, {boxed [T]}path]
23+
24
NB. u = (Path<Node>)->[Path<Node>]  extendPath
25
NB. v = (Node,Node)->num            heuristicDist
26
NB. y = Node                        start
27
NB. z = Path<Node>                  shortest path
28
aStar =: 2 : 0
29-
	newDist  =. 1 + > {. y
29+
30-
	path     =. > {: y
30+
31-
	lastPos  =. {: path
31+
	frontier =. ,<,:y
32-
	newPoses =. m gridNeighbors lastPos
32+
33-
	newPaths =. path ,"_ 1 newPoses
33+
34-
	(newDist ; <)"_1 newPaths
34+
35
		last        =. {: > currentPath
36
		if. last e. closed do. continue.            end.
37
		if. last -: x      do. >currentPath return. end.
38
		closed   =. closed, last
39
		frontier =. frontier, u currentPath
40
		NB. SORT FRONTIER
41
		scores   =. (# +  x v {:)&> frontier
42
		frontier =. frontier /: scores
43
	end.
44
	exception_message=:'No path found.' throw.
45-
	frontier =. ,: 0;<,:y
45+
46
47
exampleG =: '#' = ];._2]0 :0
48
....##..##
49-
		last        =. {: > {: currentPath
49+
.##.##..##
50-
		if. last e. closed do. continue.           end.
50+
#..#####..
51-
		if. last -: x      do. currentPath return. end.
51+
#.#...##..
52
.##...###.
53-
		frontier =. frontier, (u currentPath)
53+
.##.####.#
54
..#..##.#.
55-
		scores   =. (>@{. + (x v {:@:>@:{:))"_1 frontier
55+
###.##..##
56
#.#####..#
57
...####..#
58
)
59
60
exampleSolution =: 0 8 (((|:exampleG) gridExtendPath) aStar manhattanDistance) 5 0
61-
exampleG =: 10 10 $ (0 0 0 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0 0 1)
61+
62-
exampleSolution =: 0 8 ((exampleG gridExtendPath) aStar manhattanDistance) 5 0
62+
NB. to inspect this solution, use:
63
   '*' (<"1 exampleSolution)} '.#' {~ |:exampleG
64
65
NB. or
66
67
   '*' (<@|."1 exampleSolution)} '.#' {~ exampleG