-
-
Save snamiki1212/42646c0b150d948854a040b59163886a 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 2 | |
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) | |
|> List.flatten() | |
|> Enum.max_by(&(&1)) | |
end | |
defp walk(maze, x, y, walked, pt) do | |
walked = Map.put(walked, {x, y}, true) # walked map | |
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} -> walk(maze, x2, y2, walked, pt) # move | |
{_, ?1} -> walk(maze, x2, y2, walked, pt + 1) # move and get point | |
{_, ?G} -> pt # goal | |
{_, _} -> [] # invalid char | |
end | |
end | |
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