#250 · Grids and matrices · Beginner · 40 XP · about 10 min
A map is a list of strings of equal length, one string per row: '#' is a square of land and '.' is a square of water. Every square is 1 unit wide, and everything outside the map is water. The map holds one island (its land squares join up side to side), and the island may have lakes inside it.
Write island_perimeter(grid) that returns the length of the coastline: count every side of a land square that touches water (a lake counts too) or the edge of the map. A side shared by two land squares is inland, so it does not count.
island_perimeter(['.##.', '.#..', '###.']) # returns 14 island_perimeter(['#']) # returns 4 island_perimeter(['##', '##']) # returns 8
A single square of land has four sides of coast. In the 2 by 2 block, each square has two sides on the outside and two shared with its neighbours, so the coast is 4 x 2 = 8.
First solves only, ranked by stars and then time.