Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment