Skip to content

Instantly share code, notes, and snippets.

@maruks
Created July 21, 2015 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruks/90d2a69ba58a906979f2 to your computer and use it in GitHub Desktop.
Save maruks/90d2a69ba58a906979f2 to your computer and use it in GitHub Desktop.
July Hack Night
-module(dojo4).
-import(lists,[nth/2]).
-include_lib ("eunit/include/eunit.hrl").
-compile(export_all).
rand_list(Size) ->
lists:map(fun(X)-> random:uniform(100) end, lists:seq(1,Size)).
grid(Size) ->
lists:map(fun(X)-> rand_list(Size) end, lists:seq(1,Size)).
fix_grid(Size)->
lists:map(fun(X)-> lists:seq(1,Size) end, lists:seq(1,Size)).
multiply([H|T],[HH|TT])->
[H*HH|multiply(T,TT)];
multiply([],[])->
[].
mult_by_rows([F|REST],[FF|RREST])->
[multiply(F,FF)|mult_by_rows(REST,RREST)];
mult_by_rows([],[])->
[].
shift_col(Xs)->
lists:map(fun([F|Ys]) -> Ys ++ [F] end, Xs).
shift_col_reverse(Xs)->
lists:map(fun(F)->[lists:last(F) | lists:droplast(F)] end, Xs).
transpose([[]|_]) -> [];
transpose(M) ->
[lists:map(fun hd/1, M) | transpose(lists:map(fun tl/1, M))].
shift_diag(Xs)->
transpose(shift_col_reverse(transpose(shift_col_reverse(Xs)))).
shift_diag2(Xs)->
transpose(shift_col(transpose(shift_col_reverse(Xs)))).
calc_rows(M) ->
M1 = shift_col(M),
M2 = shift_col(M1),
M3 = shift_col(M2),
lists:max(lists:flatten(lists:foldl(fun(E,A)-> mult_by_rows(A,E) end, M, [M1,M2,M3]))).
calc_col(M) ->
calc_rows(transpose(M)).
calc_diag(M)->
M1 = shift_diag(M), M2 = shift_diag(M1), M3 = shift_diag(M2),
lists:max(lists:flatten(lists:foldl(fun(E,A)-> mult_by_rows(A,E) end, M, [M1,M2,M3]))).
calc_diag2(M)->
M1 = shift_diag2(M), M2 = shift_diag2(M1), M3 = shift_diag2(M2),
lists:max(lists:flatten(lists:foldl(fun(E,A)-> mult_by_rows(A,E) end, M, [M1,M2,M3]))).
calc(M)->
lists:max([calc_rows(M),calc_col(M),calc_diag(M),calc_diag2(M)]).
calc_test() ->
G = fix_grid(10),
?assertEqual(10000, calc(G)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment