Skip to content

Instantly share code, notes, and snippets.

@je6bmq
Last active June 16, 2018 10:00
Show Gist options
  • Save je6bmq/7a00c220a4408d037afa982a5b4b1933 to your computer and use it in GitHub Desktop.
Save je6bmq/7a00c220a4408d037afa982a5b4b1933 to your computer and use it in GitHub Desktop.
Erlang & Elixir Fest 2018 gumi quiz mission 3
defmodule Gumimaze do
def read() do
lines = "maze.txt" |> File.read!() |> String.trim() |> String.split("\n")
for {line, y} <- Enum.with_index(lines), {c, x} <- Enum.with_index(String.to_charlist(line)), into: %{} do
{{x, y}, c}
end
end
def solve3(maze) do
{x, y} = elem(Enum.find(maze, fn {_, v} -> v == ?S end), 0)
solve3_supervisor(maze, x, y)
|> Enum.max
end
defp solve3_supervisor(maze,x,y) do
_ = spawn(Gumimaze, :walk3, [self(), maze, x, y, %{}, 0])
solve3_supervisor([])
end
defp solve3_supervisor(score_list) do
receive do
{:ok, pt} ->
solve3_supervisor([pt | score_list])
{:error, _ } ->
solve3_supervisor(score_list)
after 100 ->
score_list
end
end
def walk3(parent_id, maze, x, y, walked, pt) do
walked = Map.put(walked, {x, y}, true)
for {x2, y2} <- [{x + 1, y}, {x, y + 1}, {x - 1, y}, {x, y - 1}] do
case {walked[{x2, y2}], maze[{x2, y2}]} do
{true, _} -> send(parent_id, {:error, :walked})
{_, ?W} -> send(parent_id, {:error, :wall})
{_, ?\s} -> spawn(Gumimaze, :walk3, [parent_id, maze, x2, y2, walked, pt])
{_, ?1} -> spawn(Gumimaze, :walk3, [parent_id, maze, x2, y2, walked, pt + 1])
{_, ?G} -> send(parent_id, {:ok, pt})
end
end
end
def main() do
Gumimaze.read() |> Gumimaze.solve3() |> IO.puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment