14
Your task is to find the length of the coastline of a map of islands provided in
an ASCII map. The input map will consist of 1 or more #
characters that
indicate land, and spaces which indicate water. The coastline is considered to
be any edge between land and water, including inland lakes and islands.
Your solution should be a complete program that reads in a file, a string or an array of strings, and outputs a single integer to the screen or stdout. Each input line may have leading or trailing space, and zero or more hash characters. The borders of the map are assumed to be space (water).
Lines may be of different lengths.
Examples:
Input:
##
##
Output: 8
Input:
### ###
##### #
##
Output: 26
Input:
#####
# #
# # #
# #
#####
Output: 36
This is code golf, so the smallest byte count wins.
Can we assume that the input is padded to a rectangle with spaces? – Martin Ender – 2016-02-06T10:14:43.007
Why a complete program? Usually functions are also allowed, and I don't see a good reason for this challenge to be restrictive. – nimi – 2016-02-06T11:22:34.780
@MartinBüttner, yes. I was hoping that the "The borders of the map are assumed to be space (water)" covered that. Let me know if I should make that clearer. – Logic Knight – 2016-02-06T12:17:23.117
@nimi, I understand your concern, but as the challenge is 12 hours old, there are 4 answers, and other people may be working on it right now, I am reluctant to change it. – Logic Knight – 2016-02-06T12:25:21.123
@CarpetPython no I mean whether we can assume that all lines of the input have the same length. – Martin Ender – 2016-02-06T12:54:03.167
@MartinBüttner, I see. I have edited the question to make this explicit. Lines may be of different lengths. – Logic Knight – 2016-02-06T14:49:40.897