M=‘land’
o=‘water’
world = [
[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]
]
def continent_size world, x, y
if world[y][x] != ‘land’
return 0
end
size = 1
world[y][x] = ‘counted land’
size = size + continent_size(world, x-1, y-1)
size = size + continent_size(world, x , y-1)
size = size + continent_size(world, x+1, y-1)
size = size + continent_size(world, x-1, y )
size = size + continent_size(world, x+1, y )
size = size + continent_size(world, x-1, y+1)
size = size + continent_size(world, x , y+1)
size = size + continent_size(world, x+1, y+1)
end
puts continent_size(world, 5, 5)
So this would start with 5,5 as counted land and
size = size + continent_size(world, x-1, y-1), which would be
4,4 3,3 2,2 1,1 0,0 then move on to
size = size + continent_size(world, x , y-1), which would be
5,4 5,3 5,2 5,1 5,0
next
6,4 7,3 8,2 9,1 10,0
next
4,5 3,5 2,5 1,5 0,5
next
6,5 7,5 8,5 9,5 10,5
next
4,6 3,7 2,8 1,9 0,10
next
5,6 5,7 5,8 5,9 5,10
lastly
6,6 7,7 8,8 9,9 10,10
if this is correct, point 6,3 which is land would be miss by those
recursion.
What am I missing??