Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. defmodule Gumimaze do
  2. def read() do
  3. lines = "maze.txt" |> File.read!() |> String.trim() |> String.split("\n")
  4. for {line, y} <- Enum.with_index(lines), {c, x} <- Enum.with_index(String.to_charlist(line)), into: %{} do
  5. {{x, y}, c}
  6. end
  7. end
  8.  
  9.  
  10. def solve2(maze) do
  11. {x, y} = elem(Enum.find(maze, fn {_, v} -> v == ?S end), 0)
  12.  
  13. walk2(maze, x, y, %{}, 0)
  14. |> List.flatten
  15. |> Enum.filter(fn a -> !is_atom(a) end)
  16. |> Enum.max
  17. end
  18.  
  19. defp walk2(maze, x, y, walked, pt) do
  20. walked = Map.put(walked, {x, y}, true)
  21. for {x2, y2} <- [{x + 1, y}, {x, y + 1}, {x - 1, y}, {x, y - 1}] do
  22. case {walked[{x2, y2}], maze[{x2, y2}]} do
  23. {true, _} -> :walked
  24. {_, ?W} -> :wall
  25. {_, ?\s} -> walk2(maze, x2, y2, walked, pt)
  26. {_, ?1} -> walk2(maze, x2, y2, walked, pt + 1)
  27. {_, ?G} -> pt
  28. end
  29. end
  30. end
  31.  
  32.  
  33. def main() do
  34. Gumimaze.read() |> Gumimaze.solve2() |> IO.puts
  35. end
  36. end
Add Comment
Please, Sign In to add comment