Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created March 7, 2011 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedisct1/858666 to your computer and use it in GitHub Desktop.
Save jedisct1/858666 to your computer and use it in GitHub Desktop.
Adku puzzle solver in Erlang
-module(adku).
-export([adku/1]).
adku({ Width, Height }) -> adku({ Width - 1, Height - 1 }, [ ], { 0, 0 }).
adku({ Max_X, Max_Y } = Borders, Queue, Pos) ->
InList = lists:member(Pos, Queue),
if InList -> 0;
Pos =:= { Max_X, Max_Y } -> 1;
true ->
NQueue = Queue ++ [ Pos ],
down(Borders, NQueue, Pos)
+ up(Borders, NQueue, Pos)
+ right(Borders, NQueue, Pos)
+ left(Borders, NQueue, Pos)
end.
down({ _, Max_Y }, _, { _, Max_Y }) -> 0;
down(Borders, Queue, { X, Y }) ->
adku(Borders, Queue, { X, Y + 1 }).
up(_, _, { _, 0 }) -> 0;
up(Borders, Queue, { X, Y }) ->
adku(Borders, Queue, { X, Y - 1 }).
right({ Max_X, _ }, _, { Max_X, _ }) -> 0;
right(Borders, Queue, { X, Y }) ->
adku(Borders, Queue, { X + 1, Y }).
left(_, _, { 0, _ }) -> 0;
left(Borders, Queue, { X, Y }) ->
adku(Borders, Queue, { X - 1, Y }).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment