-module(ms).
-export([score/1]).
index(XS) -> [{I,X}||{I,X}<-lists:zip(lists:seq(1,length(XS)),XS)].
score(Field) ->
Rows=string:tokens(Field,"\n"),N=length(Rows),
[FirstRow|_]=Rows,M=length(FirstRow),
Vectors=
[{-1,-1},{0,-1},{ 1,-1},
{-1, 0}, { 1, 0},
{-1, 1},{0, 1},{ 1, 1}],
At=fun(X,Y) -> Row=lists:nth(Y,Rows), lists:nth(X,Row) end,
Count=fun(FX,FY)->
Coords=[{FX+DX,FY+DY}||{DX,DY}<-Vectors],
Neighbours=[At(X,Y)||{X,Y}<-Coords,X>=1,Y>=1,X=<M,Y=<N],
Mines=[C||C<-Neighbours,C =:= $*],
length(Mines) end,
ScoreRows=
[[if
C =:= $* -> $*;
C =/= $* -> $0 + Count(X,Y)
end
||{X,C}<-index(Row)]
||{Y,Row}<-index(Rows)],
string:join(ScoreRows,"\n").