Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
2016_feb
Browse files Browse the repository at this point in the history
  • Loading branch information
samdsk committed Feb 7, 2023
1 parent a7efaad commit c9487ac
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
16 changes: 16 additions & 0 deletions erlang/exams/2016_feb_impossible/asd.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-module(asd).
-compile(export_all).

start() ->
register(server,spawn(fun()-> listen()end)).

listen() ->
receive
{ciao,Node,Pid} -> {Pid,Node} ! res, io:format("res\n"),listen()
end.

rpc(Node) ->
{server,Node} ! {ciao,node(),self()},
receive
res -> io:format("ciao ~p\n",[node()])
end.
67 changes: 67 additions & 0 deletions erlang/exams/2016_feb_impossible/hyper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-module(hyper).
-compile(export_all).
%-export([create/0]).
test()-> create(), hamilton("Hello", gray(4)).

create() ->
L = gray(4),
Neighbours = maps:from_list([ {X,find_neighbours(X,L,[])} || X <- L]),
io:format("Neighbours: ~p\n",[Neighbours]),
Pids = [{X,spawn_link(fun() -> sub(X)end)} || X <- L],
send_neigbour_pids(Pids, Pids, Neighbours),
{_,Pid} = lists:keyfind("0000",1,Pids),
register('0000',Pid).

send_neigbour_pids([],_,_) -> io:format("Sent pids\n");
send_neigbour_pids([{S,Pid}|T],Pids,Neighbours) ->
io:format("S:~p:~p\n",[S,Pid]),
L = maps:get(S,Neighbours),
Pid ! {neighbours, find_nPids(Pids, L)},
send_neigbour_pids(T,Pids,Neighbours).

find_nPids(Pids,L) -> [lists:keyfind(X,1,Pids)|| X<-L].

sub(Node) ->
%io:format("Spawned: ~p - waiting for neighbours pids\n",[Node]),
receive
{neighbours,List} -> listen(Node,List)
end.

listen(Node,List) ->
io:format("Ready: ~p -> ~p - pids ~p\n",[Node,self(),List]),
receive
{msg,Msg,[_,Next | Path]} = M ->
{_,NextPid} = lists:keyfind(Next,1,List),
io:format("Node: ~p Next: ~p Find:~p\n",[Node,Next,NextPid]),
NextPid ! {msg,{src,Node,msg,Msg},Path},listen(Node,List);
{msg,_Msg,[]} = M -> io:format("End ~p\n",M)
end.


hamilton(Msg, Path) ->
'0000' ! {msg,Msg,Path},
receive
Any -> io:format("~p\n",[Any])
end.


find_neighbours(_,[],Output) -> Output;
find_neighbours(S,[H|L],Output)->
case myxor(S,H,1,0) of
1 -> find_neighbours(S,L,[H|Output]);
_ -> find_neighbours(S,L,Output)
end.


myxor(S,R,5,Output) -> Output;
myxor(S,R,N,Output) ->
case lists:nth(N, S) =:= lists:nth(N,R) of
true -> myxor(S,R,N+1,Output);
false -> myxor(S,R,N+1,Output+1)
end.

gray(0) -> [""];
gray(N) ->
L1 = gray(N-1),
L2 = lists:reverse(L1),
["0"++X || X <- L1]++["1"++X || X<-L2].

0 comments on commit c9487ac

Please sign in to comment.