Skip to content

Instantly share code, notes, and snippets.

@mokele
Created September 27, 2012 13:52
Show Gist options
  • Save mokele/3794122 to your computer and use it in GitHub Desktop.
Save mokele/3794122 to your computer and use it in GitHub Desktop.
geometry(Size) ->
Area = Size * Size,
lists:map(
fun(N) ->
{N, neighbours(N, Size, Area)}
end,
lists:seq(1, Area)
).
neighbours(N, Size, Area) ->
lists:foldl(
fun(Direction, Acc) ->
case neighbour(Direction, N, Size, Area) of
undefined ->
Acc;
Neighbour ->
[Neighbour|Acc]
end
end,
[], [up, right, down, left]
).
neighbour(up, N, Size, _) when N > Size ->
N - Size;
neighbour(right, N, Size, _) when N rem Size =/= 0 ->
N + 1;
neighbour(down, N, Size, Area) when N =< Area - Size ->
N + Size;
neighbour(left, N, Size, _) when N rem Size =/= 1 ->
N - 1;
neighbour(_, _, _, _) ->
undefined.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment