%% File: solve.erl
%% @author Richard Carlsson

-module(solve).
-export([it/0]).

%% Problem: VIER and NEUN are 4-digit squares; determine distinct V, I,
%% E, R, N, and U, such that (given the choice of E) there exists a
%% unique solution (VIER,NEUN).

it() ->
    Qs = [integer_to_list(X*X) || X <- lists:seq(32,99)],
    Ps = [{E,{Vr,Nn}} || [N,E,_U,N]=Nn <- Qs, [_V,_I,E1,_R]=Vr <- Qs,
                         E =:= E1,
                         length(ordsets:from_list(Nn++Vr)) =:= 6],
    D = lists:foldl(fun ({E,_}, D) -> dict:update_counter(E,1,D) end,
                    dict:new(), Ps),
    [P || {E,1} <- dict:to_list(D), {E1,P} <- Ps, E=:=E1].
