Permalink
Browse files

working! exits processes when a solution is found; hack to reset memo…

…ry and not let it run forever
  • Loading branch information...
1 parent b7cc006 commit 0d2995acae4671ac9454f5bcb9b5b6803575865e @benjaminplee committed Jun 7, 2012
Showing with 74 additions and 14 deletions.
  1. +57 −0 src/word.erl
  2. +17 −14 src/word_ladder_app.erl
View
@@ -0,0 +1,57 @@
+-module(word).
+-compile([export_all]).
+
+establish(Word) ->
+ gproc:reg({n, l, Word}),
+ loop(),
+ ok.
+
+loop() ->
+ receive
+ {Link, Chain, Link} ->
+ io:format("Solution! ~p~n", [Chain]),
+ exit("Found a solution"),
+ ok;
+ {Link, Chain, Goal} when length(Chain) < 10 ->
+ find_next_link(Link, Chain, Goal),
+ loop()
+ end.
+
+find_next_link(Link, Chain, Goal) ->
+ case contains_count(Link, Chain) of
+ 1 ->
+ %io:format("Chain: ~p~n", [Chain]),
+ characters([], Link, Chain, Goal);
+ _ ->
+ noop
+ end.
+
+contains_count(X, Xs) ->
+ lists:foldl(fun(Y, Sum) ->
+ case (Y == X) of
+ true ->
+ Sum + 1;
+ false ->
+ Sum
+ end
+ end, 0, Xs).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+characters(_Prefix, [], _History, _Goal) ->
+ ok;
+characters(Prefix, [H | T], History, Goal) ->
+ candidate(Prefix, H, T, History, Goal),
+ characters(Prefix ++ [H], T, History, Goal).
+
+candidate(Prefix, Letter, Suffix, History, Goal) ->
+ candidate($a, Prefix, Letter, Suffix, History, Goal).
+
+candidate(123, _Prefix, _Letter, _Suffix, _History, _Goal) ->
+ ok;
+candidate(Letter, Prefix, Letter, Suffix, History, Goal) ->
+ candidate(Letter + 1, Prefix, Letter, Suffix, History, Goal);
+candidate(Alt, Prefix, Letter, Suffix, History, Goal) ->
+ Candidate = Prefix ++ [Alt | Suffix],
+ catch gproc:send({n, l, Candidate}, {Candidate, [Candidate | History], Goal}),
+ candidate(Alt + 1, Prefix, Letter, Suffix, History, Goal).
View
@@ -2,27 +2,30 @@
-compile([export_all]).
start() ->
- application:start(sasl),
application:start(gproc),
- initialize("four_letter_words.txt"),
ok.
-initialize(FileName) ->
- [spawn_process(X) || X <- readlines(FileName)],
- ok.
+initialize() ->
+ initialize("four_letter_words.txt").
-spawn_process(Word) ->
+initialize(FileName) ->
+ {ok, Device} = file:open(FileName, [read]),
+ establish_words(Device),
ok.
-%find(StartWord, EndWord) ->
- %word_ladder_sup:lookup_word(StartWord, [], EndWord).
+find() ->
+ find("cold", "warm").
-readlines(FileName) ->
- {ok, Device} = file:open(FileName, [read]),
- get_all_lines(Device, []).
+find(Start, End) ->
+ io:format("Finding ladder from [~p] to [~p]...~n", [Start, End]),
+ gproc:send({n, l, End}, {End, [End], Start}),
+ ok.
-get_all_lines(Device, Accum) ->
+establish_words(Device) ->
case io:get_line(Device, "") of
- eof -> file:close(Device), [lists:filter(fun(10) -> false; (_) -> true end, Line) || Line <- Accum];
- Line -> get_all_lines(Device, [Line | Accum])
+ eof -> file:close(Device);
+ Line -> erlang:spawn_link(word, establish, [strip_line_ending(Line)]), establish_words(Device)
end.
+
+strip_line_ending(Line) ->
+ lists:filter(fun(X) -> X =/= 10 end, Line).

0 comments on commit 0d2995a

Please sign in to comment.