Land perimeter
A grid of 1s and 0s shows the location of land and water. A 1 represents a square full of land, a 0 represents a square full of water. Your job is to calculate the perimeter of the land, both as it touches water and touches the edge.
A grid with a single square filled with land has a perimeter of 4, since there are four sides:
(perimeter [[1]]) ;=> 4Likewise, a single square filled with water has a perimeter of 0:
(perimeter [[0]]) ;=> 0Two squares of land next to each other share an edge, which reduces the perimeter:
(perimeter [[1 1]]) ;=> 6The edge of the grid is like an implicit encircling of water:
(perimeter [[1 1]
[1 1]]) ;=> 8
(perimeter [[0 0 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 0 0]]) ;=> 8 (same!)Here are some other weird shapes:
(perimeter [[1 1 1 1 1 1]
[1 0 0 0 0 1]
[1 0 1 1 0 1]
[1 0 0 0 0 1]
[1 1 1 1 1 1]]) ;=> 42
(perimeter [[0 1 0 0]
[1 1 1 0]
[0 1 0 0]
[1 1 0 0]]) ;=> 16Thanks to this site for the challenge idea where it is considered Expert in JavaScript. The problem has been modified from the original.
Please submit your solutions as comments on this gist.
Uh oh!
There was an error while loading. Please reload this page.