-
-
Save snamiki1212/4abcbbbe3c6da6733af95e4728c94531 to your computer and use it in GitHub Desktop.
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
# https://gist.github.com/cooldaemon/35acccd20d226ee33b639b17156242dc | |
# Mission 3 | |
defmodule Gumimaze do | |
require IEx | |
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 solve(maze) do | |
{x, y} = elem(Enum.find(maze, fn {_, v} -> v == ?S end), 0) | |
walk(maze, x, y, %{}, 0) | |
|> Enum.max_by(&(&1)) | |
end | |
defp walk(maze, x, y, walked, pt) do | |
walked = Map.put(walked, {x, y}, true) # walked map | |
[{x + 1, y}, {x, y + 1}, {x - 1, y}, {x, y - 1}] | |
|> Enum.map(fn {x2, y2} -> | |
case {walked[{x2, y2}], maze[{x2, y2}]} do | |
{true, _} -> [] # :walked | |
{_, ?W} -> [] # :wall | |
{_, ?\s} -> Task.async(fn -> walk(maze, x2, y2, walked, pt) end) # move | |
{_, ?1} -> Task.async(fn -> walk(maze, x2, y2, walked, pt + 1) end) # move and get point | |
{_, ?G} -> pt # goal | |
{_, _} -> [] # invalid char | |
end | |
end) | |
|> Enum.map(fn x -> | |
case x do | |
x = %Task{} -> Task.await(x) | |
x -> x | |
end | |
end) | |
|> List.flatten() | |
end | |
end | |
Gumimaze.read() |> Gumimaze.solve() |> IO.puts() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment