Skip to content

Instantly share code, notes, and snippets.

@najibghadri
Created October 17, 2017 22:16
Show Gist options
  • Save najibghadri/a20ff2542db1017cea42c7b76f261669 to your computer and use it in GitHub Desktop.
Save najibghadri/a20ff2542db1017cea42c7b76f261669 to your computer and use it in GitHub Desktop.
%% @type sspec() = {dist(), board()}.
%% @type dist() = integer().
%% @type field() = [info()].
%% @type info() = s | w | integer().
%% @type board() = [[field()]].
%% @type ssol() = [[integer()]].
%% @spec khf2:n_away(SSpec::sspec(), SSol::ssol()) -> B::bool().
%% B igaz, ha az SSol részlegesen kitöltött érték-mátrix teljesíti az SSpec
%% Sudoku-feladványban szereplő távolságinfók által támasztott feltételeket.
-module(khf2).
-author('najibg96@gmail.com').
-vsn('2017-10-17').
-export([n_away/2]).
n_away({_N, [[[]]]}, _) ->
true;
n_away({N, RULEM}, VALUEM) when length(RULEM) =:= length(VALUEM)->
down_n_away_check(N, w, RULEM, VALUEM) andalso down_n_away_check(N, s, transpose(RULEM), transpose(VALUEM)).
down_n_away_check(N, D, [RMH | RMT], [VMH | VMT]) ->
right_n_away_check(N, D, RMH, VMH) andalso down_n_away_check(N, D, RMT, VMT).
right_n_away_check(_N, _D, [_RULE], [_NUM]) ->
true;
right_n_away_check(N, D, [ _ , RULE | RULESTAIL], [ PREVNUM, NUM | NUMSTAIL ] ) ->
rule_check(N, D, PREVNUM, RULE, NUM) andalso right_n_away_check(N, D, [RULE]++RULESTAIL, [NUM]++NUMSTAIL).
rule_check(N, w, PREVNUM, [w | _], NUM) ->
disance_check(N, PREVNUM, NUM);
rule_check(N, w, PREVNUM, [_, w | _], NUM) ->
disance_check(N, PREVNUM, NUM);
rule_check(N, w, PREVNUM, [s | _ ], NUM) ->
not disance_check(N,PREVNUM,NUM);
rule_check(N, w, PREVNUM, [], NUM) ->
not disance_check(N,PREVNUM,NUM);
rule_check(N, s, PREVNUM, [s | _ ], NUM) ->
disance_check(N, PREVNUM, NUM);
rule_check(N, s, PREVNUM, [w | _], NUM) ->
not disance_check(N,PREVNUM,NUM);
rule_check(N, s, PREVNUM, [_, w | _], NUM) ->
not disance_check(N,PREVNUM,NUM);
rule_check(N, s, PREVNUM, [], NUM) ->
not disance_check(N,PREVNUM,NUM).
disance_check(N, PREVNUM, NUM) ->
N =:= abs(PREVNUM-NUM).
transpose([[]|_]) -> [];
transpose(M) ->
[lists:map(fun hd/1, M) | transpose(lists:map(fun tl/1, M))].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment