I am trying to take in a 2D array, search for any space that is greater than 0, then increment the surrounding spaces by 1.
array = [[0,0,0], [0,1,0], [0,0,0]]
The final output should look like this
new_array = [[0,1,0], [1,1,1], [0,1,0]]
The way I have it set up currently is returning
[1,1,1,1]
It is incrementing the correct spaces, I think, I just am not sure how to put them back into the original arrays and then return the 2D array. Clearly some steps are missing, any help would be greatly appreciated. I understand why it is returning the way it is, just not clear on the next step.
def dilate(array)
new_array = []
array.each_with_index do |row, rowIndex|
row.each_with_index do |col, colIndex|
if (array[rowIndex][colIndex] > 0)
new_array << (array[rowIndex][colIndex -1] +1)
new_array << (array[rowIndex -1][colIndex] +1)
new_array << (array[rowIndex][colIndex +1] +1)
new_array << (array[rowIndex +1][colIndex] +1)
end
end
end
return new_array
end
