• I have no clue how to approach...

Question related to mission Radiation Search

 

I tried using recursion, but it would only travel/seek in one direction and it would end up missing a couple, especially on the second example. I saw the suggestion on another post, about union functions, but it wasn't clear to me how to apply that to this case. I just reset my code for the fourth time out of frustration, so I'm back to the drawing board. Any push in a better direction would be appreciated.