Created
June 16, 2018 09:55
-
-
Save je6bmq/407dfcdc72c0de1185a63a69329c0a33 to your computer and use it in GitHub Desktop.
Erlang & Elixir Fest 2018 gumi quiz mission 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 solve2(maze) do | |
{x, y} = elem(Enum.find(maze, fn {_, v} -> v == ?S end), 0) | |
walk2(maze, x, y, %{}, 0) | |
|> List.flatten | |
|> Enum.filter(fn a -> !is_atom(a) end) | |
|> Enum.max | |
end | |
defp walk2(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, _} -> :walked | |
{_, ?W} -> :wall | |
{_, ?\s} -> walk2(maze, x2, y2, walked, pt) | |
{_, ?1} -> walk2(maze, x2, y2, walked, pt + 1) | |
{_, ?G} -> pt | |
end | |
end | |
end | |
def main() do | |
Gumimaze.read() |> Gumimaze.solve2() |> IO.puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment