Skip to content

Instantly share code, notes, and snippets.

@linkin-park
Created September 11, 2017 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkin-park/190e1733a0d7e20f317c2e16b8b47dc5 to your computer and use it in GitHub Desktop.
Save linkin-park/190e1733a0d7e20f317c2e16b8b47dc5 to your computer and use it in GitHub Desktop.
-module(solution).
-export([main/0]).
main() ->
Arr = read_2darray(6,6,"~d"),
io:format("~w~n",[lists:max(groupSum(Arr,1,1,[]))]).
%taken in reverse order
read_array(0,_) -> [];
read_array(N,D) ->
{ok, [X]} = io:fread("", D),
[X | read_array(N-1,D)].
read_2darray(0,_,_) -> [];
read_2darray(N,M,D) ->
Q=read_array(M,D),
[Q | read_2darray(N-1,M,D)].
%done in order fashion
groupSum([],_,_,Res) -> Res;
groupSum(_,R,_,Res) when (R > 4) -> Res;
groupSum(Arr,R,C,Res) when (C > 4) -> groupSum(Arr,R+1,1,Res);
groupSum(Arr,R,C,Res) -> V = sum(Arr,[{R,C},{R,C+1},{R,C+2},{R+1,C+1},{R+2,C},{R+2,C+1},{R+2,C+2}],0),
groupSum(Arr,R,C+1,[V|Res]).
sum(_,[],SumRes) -> SumRes;
%first get row than get column in the given row
sum(Arr,[{A,B}|Xs],SumRes) -> sum(Arr,Xs,lists:nth(B,lists:nth(A,Arr))+SumRes).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment