[Question]
  [
# Roguelike pathfinding
Your task will be, given a two-dimensional array of the elements described below, which represents a dungeon, to output or return a single number representing the amount of gold pieces the rogue can collect without waking up any monsters.
The elements of the array are as follows:
1. Empty spaces are represented by either `.` or a space, your call;
2. Rogue's starting position is represented by, of course, `@`;
3. A gold piece is represented by `$`;
4. Walls are represented by `#`;
5. Monsters are represented by characters from the following regexp: `[a-zA-Z*&]`.
The array shall not contain any characters not listed above, so you can assume that anything that is not a wall, an empty space, the rogue or a gold piece is a monster.
The rules for pathfinding are:
1. The rogue can only walk through empty cells or cells containing gold;
2. It takes a turn to move to a adjacent or diagonally adjacent cell;
3. Picking up the gold is instant;
4. The rogue can't stay adjacent or diagonally adjacent to a monster for more than one turn without waking it up, which is forbidden;
5. The rogue can enter the awareness area of a monster any number of times, the monster will only wake up if the rogue spends two *consecutive* turns near it.
# Input and output rules
You can get the input in any reasonable format, including a two-dimensional array, a flat array, a string or whatever else. If it makes your life easier, you may also take the dimensions of the array as well.
It's guaranteed that the rogue will not be near a monster at the beginning.
A full program or a function is fine.
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), the score is the bytes count of your submission with fewer being better.
# Test cases
I use dots for empty spaces here for readability purposes, if you so desire you may use spaces (see above). Also note that this is a pure coincidence that the rogue is always in the upper-left corner, your code should handle any other valid position as well.
```
1)
@..
.$.
...  -> 1
```
Just a sanity test.
```
2)
@....
...g$
.....  -> 0
```
Again, a sanity test.
```
3)
@....
...$g
.....  -> 1
```
The rogue can grab the gold by moving in from the left.
```
4)
@....g..
.......$
........
.....h..  -> 1
```
The rogue can zig-zag between the monsters, never staying for more than one turn near each.
```
5)
@....z..
.......$
.....b..  -> 0
```
The tactics from the previous test case don't work here - the monster sensitivity areas overlap.
```
6)
@$#.
###$
....  -> 1
```
Sanity test.
```
7)
@..#..
$.$g.$
...#..  -> 2
```
Ditto.
```
8)
@#.d#$
$...##
e.....
..$...
##..$b
.#..g$  -> 3
```
Of all the gold here, only three can be reached safely: the gold near the starting position can be got by moving down one and then back to the starting position. To escape from the top left corner the rogue has to move diagonally down-right twice. The gold in the middle poses no challenge. The outer gold guarded by `g` and `b` can be got by moving in diagonally from the place to the right of the middle gold and then back. The rest cannot be got: top-right gold is blocked by walls, and the bottom-right gold requires two turns in monster sensitivity areas.
The following test cases were generously donated by mbomb007.
```
9)
  12345678
a @....g.D
b .......$
c ......#.
d .....h..  -> 1
```
This one is tricky. A path is `b4-b5-c6-b7-c8-b8(grab)`.
```
10)
  12345678
a @....g.D
b .......$
c .......#
d .....h..  -> 1
```
A path is `[bc]4-c5-b6-c7-b8(grab)`.
```
11)
  12345678
a @....g.D
b ......#$
c .......#
d .....h..  -> 1
```
The extra wall doesn't actually change anything, `[bc]4-c5-b6-c7-b8(grab)` is still a solution.
  
[Answer]
previous solutions (part of them) can be found in the footer container in the tio link (those are probably more readable)
# [JavaScript (Node.js)](https://nodejs.org),883 436 411 360,345 311 bytes
```
g=>g.map((r,y)=>[...r].map((c,x)=>A+=c=="$"&&P(g,x,y)),A=0)|A
P=(g,x,y,m={},p={},I=i=9,M={})=>{if(/[.$]/.test(c=(g[y]||0)[x])){for(;i--;)if(/^[^.@$#]$/.test(C=(g[y+~-(i/3)]||0)[x+i%3-1])){if(m[C])return
M[C]=1}for(;I--;)if(!p[(X=x+~-(I/3))+","+(Y=y+I%3-1)]&&P(g,X,Y,M,{...p,[x+","+y]:1}))return 1}return c=="@"}
```
[Try it online!](https://tio.run/##zVVdb9pAEHzfX3Gx3egOnw8c0kotvSiIvPCAFKUvSS1HEDCOER@WbSII0L9O92yjBAcikgqplmzt3e3M7s1q5EHnqRN3oyBMrPGk5637cu3LC1@MOiGlEZ8zeeEIISI32@nyGe7UTdmVUjO009Nr6vMZpjFelxW2rMO1zHb4SC5WPFSfpgzkd97CELGLoE/LjjDcski8OKFdzHfm7nJZYc7MZWzRn0S0FlhWjanMe@deXBq6a@TpjTTd/GPRoFxlOcwMvlQtW4ERMnIaLou8ZBqNoYWxtFcpZTOnPAkdeitniqKJFMzUuGbSOzk3m4qFudmlbvkdb/EF3j3kWEElzd0f9orl3MRe5YGS4lJbrbuTcTwZemI48Wmfti@FAGHgK0RbxOEwSNrQZlLaDOBtqkgTfUN9C4DKOwDD3wHYV8HPMOoxNkG@83ggxfMbiocDujV0AbquZ4gD6uhYxcC7ZTX0AuZsB0YXPaQ3VLYO3uZiao2FMXgAReMbW0TVvTpdFXXSP6jTDgqhQ498kkR/RbKXolyC6vlXgjTTJJiM4cg@JukD@@zsuJmf4ZWhIXN0x3quW79LpweaGgquJv3OMPY23s66eHE4FCxOyuQfTJ5EUw9WsO10gOq3Cvl1RJ0/oXIqQSCts5ppBj/PMiFupNI1cFl6OshOB5vT3aO4QSEG/6fs57Z9BNkdF2VHIT@idp741IlIC7GfEf9F@4L0y6UmNPyXASFqAiIYd4fTnhfTBtseBSEtEU7jRzyAVwNRTW31stkstnASvnBTrJyPC3tmLJfMHHBc4rCcdFibWb07KFxDqQzrvw "JavaScript (Node.js) – Try It Online")
# Explantion -
instead of going from the player to the cash i went from the case to the @. i think i should be faster because i know when to stop looking (reach the @) , and when you look for cash you need to always keep moving until you covered all of the spots (and the ways to get to them). so the algo is pretty simple that way - the main function 
`g.map((r,y)=>[...r].map((c,x)=>A+=c=="$"&&P(g,x,y)),A=0)|A`:
find cash -> if you found it -> start looking for the player -> if you found him -> increment A
now lets got to the path finder aka P
`if(/[.$]/.test(c=(g[y]||[])[x]))` just check if the current cell is "special" -> if so we want to return if its a player. special cases : @#(monster)
`for(;i--;)
if(/^[a-zA-Z*&]$/.test(C=(g[y+~-(i/3)]||0)[x+i%3-1])) -> if my neighbor is a monster
{if(m[C])return false -> and it already was in the previous turn - this path is not value
M[C]=1} -> if not just add it to the neighbors monsters`
`for(;I--;)
if(!p[(X=x+~-(I / 3))+","+(Y=y+I%3-1)]&&P(g,X,Y,M,{...p,[x+","+y]:1}))return true` iterate neighbors again - if i wasn't already there (p is the path taken)
continue path (call P)
[Answer]
# Python3, 710 bytes
```
E=enumerate
def F(s,c,C,O,e,M,x,y,U):
 r=[]
 for X,Y in s:e[(x,y)]=e.get((x,y),[])+[((X,Y),c)];r+=[(O+[(x,y)],X,Y,c+C*((X,Y)not in U),M,U+[(X,Y)]*C)]
 return r
def f(b):
 d,D={4:[]},[]
 for x,r in E(b):
  for y,v in E(r):d[K]=d.get(K:=[[[[4,3][v=='$'],2][v=='#'],1][v=='.'],0]['@'==v],[])+[(x,y)];D+=[(x,y)]
 C,e=[],{}
 q=[([],*d[0][0],0,set(),[])]
 while q:
  O,x,y,c,M,U=q.pop(0)
  if c==len(d[3]):return c
  o={a:[(x+X,y+Y)for X,Y in[(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]if(P:=(x+X,y+Y))in b and[]==[1 for(J,K),w in e.get((x,y),[])if(J,K)==P and w>=c]]for a,b in d.items()}
  if not M&{*o[4]}:C+=[c];q=F(o[3],c,1,*(P:=[O,e,{*o[4]},x,y,U]))+q+F(o[1],c,0,*P)+F(o[0],c,0,*P)
 return max(C+[0])
```
[Try it online!](https://tio.run/##lVRtT9swEP68/AqLRKvdHFbSFihBnpBa@DDE4AsSyLOmNnFLpZK0SXjpqv727pyXwiCbtki1757nufPdxelild8ncbe/SLfbM6HjxwedjnJtRXpCzmkGIQzgCjRcwgus4IYFFkmFVBaZJCm5hTsyi0kWaEmRZkpoPtU5LRyQirmSUhQxCJk6SV0h6ZVbSQFxCN1Bu1TESW5S3TA86QY1BlPtAcOTUp0/pjFJi5omdGxqiGAo1r1Aqg3UxbxAajKclYICWsFTCaUsiOSFElFR3kUgJD496Cr5JETLaSnolKaNpl@aHE1PydZpS4gnVXVTlH4yNJ0UpkUGoHEesN5YZIkomu1IYpyH0ZDhacUgUPh8P5trsjS1XRXDDE2rYskXyYJ6DOHZhIRCzHVMI9lVLKgaD5FKxHoU4JnuLazcO/Y6fEk98Bngum82Hzxc98vNhxIqmP3S2y9cNZvQ60Ds8jGc0piM4kgqIaRvhke/wgWDZzO/dy8VYw0nxLWJIM9fRKiUqWgEYyOP@CzXDxllm7In82ovP6/bieypTTDA2YXqZCnOaYJd4hR8aJtipLlmlaq8bIoxd@kanW90HrSvWeF6O3d3Ox5GL3TgIsO2mU8E2dvbs045t7iDP9yNn3VeCV7AU8esNd19TzvTt3TvLT0tFeZxaqNC7uuAg7cBPz8EjGvdYa1zbG7Ztl3yJXf0msNGzMGayni7VvRrhc0jDHUMZ1u6Lsj4mBSNsWWCsOUi7LgK49zvdHsHh0d9a8Sr1obWmO9qDSsLa4v47w363v8m4fbHJP4/JbH/lsT8M@TJj3EySiM6xO//U3UtZHsym@c6pd@SWAMZ8mwxn@W09T1uMfwoF@kszumE7mIznzHWAHea4W4z3GuGD5rhw2b4qBnuN8PHzbDv/QE3fW5/AQ)
]